Skip to content

Handle when associated name can be undefined #41285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

....? This error seems.... odd. Why are we even comparing non-this parameter types (one of the signatures doesn't have any non-this parameters)?


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);