-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Discriminate contextual types #19733
Changes from 1 commit
ce105a0
dd45e0f
8799cf4
4467801
24e2587
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13663,8 +13663,33 @@ namespace ts { | |
// Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily | ||
// be "pushed" onto a node using the contextualType property. | ||
function getApparentTypeOfContextualType(node: Expression): Type { | ||
const type = getContextualType(node); | ||
return type && getApparentType(type); | ||
let contextualType = getContextualType(node); | ||
contextualType = contextualType && getApparentType(contextualType); | ||
if (contextualType && contextualType.flags & TypeFlags.Union && isObjectLiteralExpression(node)) { | ||
let match: Type | undefined; | ||
propLoop: for (const prop of node.properties) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code is almost exactly the same as the also-highly-questionable
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh hey, waddoyaknow, we already do have a test ( |
||
if (!prop.symbol) continue; | ||
if (prop.kind !== SyntaxKind.PropertyAssignment) continue; | ||
if (isDiscriminantProperty(contextualType, prop.symbol.escapedName)) { | ||
const discriminatingType = getTypeOfNode(prop.initializer); | ||
for (const type of (contextualType as UnionType).types) { | ||
const targetType = getTypeOfPropertyOfType(type, prop.symbol.escapedName); | ||
if (targetType && checkTypeAssignableTo(discriminatingType, targetType, /*errorNode*/ undefined)) { | ||
if (match) { | ||
if (type === match) continue; // Finding multiple fields which discriminate to the same type is fine | ||
match = undefined; | ||
break propLoop; | ||
} | ||
match = type; | ||
} | ||
} | ||
} | ||
} | ||
if (match) { | ||
contextualType = match; | ||
} | ||
} | ||
return contextualType; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//// [contextuallyTypedByDiscriminableUnion.ts] | ||
type ADT = { | ||
kind: "a", | ||
method(x: string): number; | ||
} | { | ||
kind: "b", | ||
method(x: number): string; | ||
}; | ||
|
||
|
||
function invoke(item: ADT) { | ||
if (item.kind === "a") { | ||
item.method(""); | ||
} | ||
else { | ||
item.method(42); | ||
} | ||
} | ||
|
||
invoke({ | ||
kind: "a", | ||
method(a) { | ||
return +a; | ||
} | ||
}); | ||
|
||
|
||
//// [contextuallyTypedByDiscriminableUnion.js] | ||
function invoke(item) { | ||
if (item.kind === "a") { | ||
item.method(""); | ||
} | ||
else { | ||
item.method(42); | ||
} | ||
} | ||
invoke({ | ||
kind: "a", | ||
method: function (a) { | ||
return +a; | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
=== tests/cases/compiler/contextuallyTypedByDiscriminableUnion.ts === | ||
type ADT = { | ||
>ADT : Symbol(ADT, Decl(contextuallyTypedByDiscriminableUnion.ts, 0, 0)) | ||
|
||
kind: "a", | ||
>kind : Symbol(kind, Decl(contextuallyTypedByDiscriminableUnion.ts, 0, 12)) | ||
|
||
method(x: string): number; | ||
>method : Symbol(method, Decl(contextuallyTypedByDiscriminableUnion.ts, 1, 14)) | ||
>x : Symbol(x, Decl(contextuallyTypedByDiscriminableUnion.ts, 2, 11)) | ||
|
||
} | { | ||
kind: "b", | ||
>kind : Symbol(kind, Decl(contextuallyTypedByDiscriminableUnion.ts, 3, 5)) | ||
|
||
method(x: number): string; | ||
>method : Symbol(method, Decl(contextuallyTypedByDiscriminableUnion.ts, 4, 14)) | ||
>x : Symbol(x, Decl(contextuallyTypedByDiscriminableUnion.ts, 5, 11)) | ||
|
||
}; | ||
|
||
|
||
function invoke(item: ADT) { | ||
>invoke : Symbol(invoke, Decl(contextuallyTypedByDiscriminableUnion.ts, 6, 2)) | ||
>item : Symbol(item, Decl(contextuallyTypedByDiscriminableUnion.ts, 9, 16)) | ||
>ADT : Symbol(ADT, Decl(contextuallyTypedByDiscriminableUnion.ts, 0, 0)) | ||
|
||
if (item.kind === "a") { | ||
>item.kind : Symbol(kind, Decl(contextuallyTypedByDiscriminableUnion.ts, 0, 12), Decl(contextuallyTypedByDiscriminableUnion.ts, 3, 5)) | ||
>item : Symbol(item, Decl(contextuallyTypedByDiscriminableUnion.ts, 9, 16)) | ||
>kind : Symbol(kind, Decl(contextuallyTypedByDiscriminableUnion.ts, 0, 12), Decl(contextuallyTypedByDiscriminableUnion.ts, 3, 5)) | ||
|
||
item.method(""); | ||
>item.method : Symbol(method, Decl(contextuallyTypedByDiscriminableUnion.ts, 1, 14)) | ||
>item : Symbol(item, Decl(contextuallyTypedByDiscriminableUnion.ts, 9, 16)) | ||
>method : Symbol(method, Decl(contextuallyTypedByDiscriminableUnion.ts, 1, 14)) | ||
} | ||
else { | ||
item.method(42); | ||
>item.method : Symbol(method, Decl(contextuallyTypedByDiscriminableUnion.ts, 4, 14)) | ||
>item : Symbol(item, Decl(contextuallyTypedByDiscriminableUnion.ts, 9, 16)) | ||
>method : Symbol(method, Decl(contextuallyTypedByDiscriminableUnion.ts, 4, 14)) | ||
} | ||
} | ||
|
||
invoke({ | ||
>invoke : Symbol(invoke, Decl(contextuallyTypedByDiscriminableUnion.ts, 6, 2)) | ||
|
||
kind: "a", | ||
>kind : Symbol(kind, Decl(contextuallyTypedByDiscriminableUnion.ts, 18, 8)) | ||
|
||
method(a) { | ||
>method : Symbol(method, Decl(contextuallyTypedByDiscriminableUnion.ts, 19, 14)) | ||
>a : Symbol(a, Decl(contextuallyTypedByDiscriminableUnion.ts, 20, 11)) | ||
|
||
return +a; | ||
>a : Symbol(a, Decl(contextuallyTypedByDiscriminableUnion.ts, 20, 11)) | ||
} | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
=== tests/cases/compiler/contextuallyTypedByDiscriminableUnion.ts === | ||
type ADT = { | ||
>ADT : ADT | ||
|
||
kind: "a", | ||
>kind : "a" | ||
|
||
method(x: string): number; | ||
>method : (x: string) => number | ||
>x : string | ||
|
||
} | { | ||
kind: "b", | ||
>kind : "b" | ||
|
||
method(x: number): string; | ||
>method : (x: number) => string | ||
>x : number | ||
|
||
}; | ||
|
||
|
||
function invoke(item: ADT) { | ||
>invoke : (item: ADT) => void | ||
>item : ADT | ||
>ADT : ADT | ||
|
||
if (item.kind === "a") { | ||
>item.kind === "a" : boolean | ||
>item.kind : "a" | "b" | ||
>item : ADT | ||
>kind : "a" | "b" | ||
>"a" : "a" | ||
|
||
item.method(""); | ||
>item.method("") : number | ||
>item.method : (x: string) => number | ||
>item : { kind: "a"; method(x: string): number; } | ||
>method : (x: string) => number | ||
>"" : "" | ||
} | ||
else { | ||
item.method(42); | ||
>item.method(42) : string | ||
>item.method : (x: number) => string | ||
>item : { kind: "b"; method(x: number): string; } | ||
>method : (x: number) => string | ||
>42 : 42 | ||
} | ||
} | ||
|
||
invoke({ | ||
>invoke({ kind: "a", method(a) { return +a; }}) : void | ||
>invoke : (item: ADT) => void | ||
>{ kind: "a", method(a) { return +a; }} : { kind: "a"; method(a: string): number; } | ||
|
||
kind: "a", | ||
>kind : string | ||
>"a" : "a" | ||
|
||
method(a) { | ||
>method : (a: string) => number | ||
>a : string | ||
|
||
return +a; | ||
>+a : number | ||
>a : string | ||
} | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// @noImplicitAny: true | ||
type ADT = { | ||
kind: "a", | ||
method(x: string): number; | ||
} | { | ||
kind: "b", | ||
method(x: number): string; | ||
}; | ||
|
||
|
||
function invoke(item: ADT) { | ||
if (item.kind === "a") { | ||
item.method(""); | ||
} | ||
else { | ||
item.method(42); | ||
} | ||
} | ||
|
||
invoke({ | ||
kind: "a", | ||
method(a) { | ||
return +a; | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invert this I think