Skip to content

Support custom 'Symbol.hasInstance' methods when narrowing 'instanceof' #55052

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

Merged
merged 15 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
163 changes: 147 additions & 16 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3663,6 +3663,18 @@
"category": "Error",
"code": 2854
},
"The left-hand side of an 'instanceof' expression must be assignable to the first argument of the right-hand side's '[Symbol.hasInstance]' method.": {
"category": "Error",
"code": 2855
},
"An object's '[Symbol.hasInstance]' method must return a boolean value for it to be used on the right-hand side of an 'instanceof' expression.": {
"category": "Error",
"code": 2856
},
"The right-hand side of an 'instanceof' expression must be either of type 'any', an object type with a '[Symbol.hasInstance]()' method, or a type assignable to the 'Function' interface type.": {
"category": "Error",
"code": 2857
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6212,11 +6212,12 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
//

// @api
function createSyntheticExpression(type: Type, isSpread = false, tupleNameSource?: ParameterDeclaration | NamedTupleMember) {
function createSyntheticExpression(type: Type, isSpread = false, tupleNameSource?: ParameterDeclaration | NamedTupleMember, thisArgument?: LeftHandSideExpression) {
const node = createBaseNode<SyntheticExpression>(SyntaxKind.SyntheticExpression);
node.type = type;
node.isSpread = isSpread;
node.tupleNameSource = tupleNameSource;
node.thisArgument = thisArgument;
return node;
}

Expand Down
10 changes: 8 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2453,11 +2453,12 @@ export interface YieldExpression extends Expression {
readonly expression?: Expression;
}

export interface SyntheticExpression extends Expression {
export interface SyntheticExpression extends LeftHandSideExpression {
readonly kind: SyntaxKind.SyntheticExpression;
readonly isSpread: boolean;
readonly type: Type;
readonly tupleNameSource?: ParameterDeclaration | NamedTupleMember;
readonly thisArgument?: LeftHandSideExpression;
}

// see: https://tc39.github.io/ecma262/#prod-ExponentiationExpression
Expand Down Expand Up @@ -6499,6 +6500,11 @@ export interface PromiseOrAwaitableType extends ObjectType, UnionType {
awaitedTypeOfType?: Type;
}

/** @internal */
export interface HasInstanceMethodType extends Type {
hasSimpleUnrestrictedSingleCallSignature?: boolean;
}

/** @internal */
export interface SyntheticDefaultModuleType extends Type {
syntheticType?: Type;
Expand Down Expand Up @@ -8783,7 +8789,7 @@ export interface NodeFactory {
//
// Synthetic Nodes
//
/** @internal */ createSyntheticExpression(type: Type, isSpread?: boolean, tupleNameSource?: ParameterDeclaration | NamedTupleMember): SyntheticExpression;
/** @internal */ createSyntheticExpression(type: Type, isSpread?: boolean, tupleNameSource?: ParameterDeclaration | NamedTupleMember, thisArgument?: LeftHandSideExpression): SyntheticExpression;
/** @internal */ createSyntaxList(children: Node[]): SyntaxList;

//
Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,7 @@ function isLeftHandSideExpressionKind(kind: SyntaxKind): boolean {
case SyntaxKind.MetaProperty:
case SyntaxKind.ImportKeyword: // technically this is only an Expression if it's in a CallExpression
case SyntaxKind.MissingDeclaration:
case SyntaxKind.SyntheticExpression: // synthetic expressions are only used by the checker to substitute specific types for expression positions, so their precedence does not matter.
return true;
default:
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ import {
TypeChecker,
TypeFlags,
TypeReference,
unescapeLeadingUnderscores,
unescapeLeadingUnderscores
} from "./_namespaces/ts";

/** @internal */
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5358,11 +5358,12 @@ declare namespace ts {
readonly asteriskToken?: AsteriskToken;
readonly expression?: Expression;
}
interface SyntheticExpression extends Expression {
interface SyntheticExpression extends LeftHandSideExpression {
readonly kind: SyntaxKind.SyntheticExpression;
readonly isSpread: boolean;
readonly type: Type;
readonly tupleNameSource?: ParameterDeclaration | NamedTupleMember;
readonly thisArgument?: LeftHandSideExpression;
}
type ExponentiationOperator = SyntaxKind.AsteriskAsteriskToken;
type MultiplicativeOperator = SyntaxKind.AsteriskToken | SyntaxKind.SlashToken | SyntaxKind.PercentToken;
Expand Down
Loading