Skip to content

Commit

Permalink
Handle when associated name can be undefined
Browse files Browse the repository at this point in the history
Fixes #40251
  • Loading branch information
sheetalkamat committed Oct 27, 2020
1 parent 9ed608b commit 268a409
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10081,7 +10081,7 @@ namespace ts {
const associatedNames = restType.target.labeledElementDeclarations;
const restParams = map(elementTypes, (t, i) => {
// Lookup the label from the individual tuple passed in before falling back to the signature `rest` parameter name
const tupleLabelName = !!associatedNames && getTupleElementLabel(associatedNames[i]);
const tupleLabelName = !!associatedNames?.[i] && getTupleElementLabel(associatedNames[i]);
const name = tupleLabelName || getParameterNameAtPosition(sig, restIndex + i, restType);
const flags = restType.target.elementFlags[i];
const checkFlags = flags & ElementFlags.Variable ? CheckFlags.RestParameter :
Expand Down Expand Up @@ -28672,7 +28672,7 @@ namespace ts {
if (isTupleType(restType)) {
const associatedNames = (<TupleType>(<TypeReference>restType).target).labeledElementDeclarations;
const index = pos - paramCount;
return associatedNames && getTupleElementLabel(associatedNames[index]) || restParameter.escapedName + "_" + index as __String;
return associatedNames?.[index] && getTupleElementLabel(associatedNames[index]) || restParameter.escapedName + "_" + index as __String;
}
return restParameter.escapedName;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/parameterNamesInRestParameter.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/index.js(2,3): error TS2684: The 'this' context of type '<T>(...items: T[]) => T[]' is not assignable to method's 'this' of type '(this: () => void) => unknown[]'.
Types of parameters 'items' and 'args_0' are incompatible.
Type 'any' is not assignable to type 'never'.


==== /index.js (1 errors) ====
function F() { }
!(Array.of.call(F) instanceof F);
~~~~~~~~
!!! error TS2684: The 'this' context of type '<T>(...items: T[]) => T[]' is not assignable to method's 'this' of type '(this: () => void) => unknown[]'.
!!! error TS2684: Types of parameters 'items' and 'args_0' are incompatible.
!!! error TS2684: Type 'any' is not assignable to type 'never'.

9 changes: 9 additions & 0 deletions tests/baselines/reference/parameterNamesInRestParameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [index.js]
function F() { }
!(Array.of.call(F) instanceof F);


//// [index.js]
"use strict";
function F() { }
!(Array.of.call(F) instanceof F);
13 changes: 13 additions & 0 deletions tests/baselines/reference/parameterNamesInRestParameter.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== /index.js ===
function F() { }
>F : Symbol(F, Decl(index.js, 0, 0))

!(Array.of.call(F) instanceof F);
>Array.of.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>Array.of : Symbol(ArrayConstructor.of, Decl(lib.es2015.core.d.ts, --, --))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>of : Symbol(ArrayConstructor.of, Decl(lib.es2015.core.d.ts, --, --))
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --))
>F : Symbol(F, Decl(index.js, 0, 0))
>F : Symbol(F, Decl(index.js, 0, 0))

17 changes: 17 additions & 0 deletions tests/baselines/reference/parameterNamesInRestParameter.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== /index.js ===
function F() { }
>F : () => void

!(Array.of.call(F) instanceof F);
>!(Array.of.call(F) instanceof F) : boolean
>(Array.of.call(F) instanceof F) : boolean
>Array.of.call(F) instanceof F : boolean
>Array.of.call(F) : unknown[]
>Array.of.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>Array.of : <T>(...items: T[]) => T[]
>Array : ArrayConstructor
>of : <T>(...items: T[]) => T[]
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R
>F : () => void
>F : () => void

9 changes: 9 additions & 0 deletions tests/cases/compiler/parameterNamesInRestParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @lib: es6
// @allowJs: true
// @checkJs: true
// @outDir: out
// @strict: true

// @filename: /index.js
function F() { }
!(Array.of.call(F) instanceof F);

0 comments on commit 268a409

Please sign in to comment.