diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1b0904723df..1e1f9b27967 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21773,9 +21773,14 @@ namespace ts { return getTypeOfParameter(signature.parameters[pos]); } if (signature.hasRestParameter) { + // We want to return the value undefined for an out of bounds parameter position, + // so we need to check bounds here before calling getIndexedAccessType (which + // otherwise would return the type 'undefined'). const restType = getTypeOfSymbol(signature.parameters[paramCount]); - const indexType = getLiteralType(pos - paramCount); - return getIndexedAccessType(restType, indexType); + const index = pos - paramCount; + if (!isTupleType(restType) || restType.target.hasRestElement || index < (restType.typeArguments || emptyArray).length) { + return getIndexedAccessType(restType, getLiteralType(index)); + } } return undefined; } diff --git a/tests/baselines/reference/getParameterNameAtPosition.errors.txt b/tests/baselines/reference/getParameterNameAtPosition.errors.txt deleted file mode 100644 index 74a31fd3def..00000000000 --- a/tests/baselines/reference/getParameterNameAtPosition.errors.txt +++ /dev/null @@ -1,20 +0,0 @@ -tests/cases/compiler/getParameterNameAtPosition.ts(9,7): error TS2345: Argument of type 'Mock<[any]>' is not assignable to parameter of type 'Tester'. - Types of parameters 'args_1' and 'done' are incompatible. - Type '(...args: any[]) => any' is not assignable to type 'undefined'. - - -==== tests/cases/compiler/getParameterNameAtPosition.ts (1 errors) ==== - // Repro from #30171 - - interface Mock extends Function { - (...args: Y): any; - } - type Tester = (opts: any, done: (...args: any[]) => any) => any; - declare function cases(tester: Tester): void; - declare function fn(implementation?: (...args: Y) => any): Mock; - cases(fn(opts => { })); - ~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type 'Mock<[any]>' is not assignable to parameter of type 'Tester'. -!!! error TS2345: Types of parameters 'args_1' and 'done' are incompatible. -!!! error TS2345: Type '(...args: any[]) => any' is not assignable to type 'undefined'. - \ No newline at end of file