-
Notifications
You must be signed in to change notification settings - Fork 706
fix(checker): apply discriminant-based contextual typing for object literals (fixes #1727) #1734
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
Open
arash-mosavi
wants to merge
11
commits into
microsoft:main
Choose a base branch
from
arash-mosavi:fix-1727-contextual-typing-visit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+442
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8485fc0
fix(checker): contextual typing by discriminant in object literals (f…
arash-mosavi 86c8830
Update internal/checker/inference.go
arash-mosavi f3c8dca
fix(checker): improve inference handling for indexed access types
arash-mosavi 94f61a6
Add test case and baselines for cssTreeTypeInference
arash-mosavi 71a1379
Update internal/checker/inference.go
arash-mosavi ac20851
Update internal/checker/inference.go
arash-mosavi 5a88d66
fix(checker): use TypeFlagsTypeVariable for consistency with getInfer…
arash-mosavi e500bd5
perf(checker): optimize blockedStringType check in union member loop
arash-mosavi 089ee80
Update internal/checker/inference.go
arash-mosavi 7dfac8f
Update internal/checker/inference.go
arash-mosavi 57f99f3
Update internal/checker/inference.go
arash-mosavi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
testdata/baselines/reference/compiler/cssTreeTypeInference.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//// [tests/cases/compiler/cssTreeTypeInference.ts] //// | ||
|
||
//// [cssTreeTypeInference.ts] | ||
// Simplified reproduction of css-tree type inference issue | ||
// https://github.com/microsoft/typescript-go/issues/1727 | ||
|
||
interface Declaration { | ||
type: 'Declaration'; | ||
property: string; | ||
value: string; | ||
} | ||
|
||
interface Rule { | ||
type: 'Rule'; | ||
selector: string; | ||
children: Declaration[]; | ||
} | ||
|
||
type ASTNode = Declaration | Rule; | ||
|
||
interface WalkOptions<T extends ASTNode> { | ||
visit: T['type']; | ||
enter(node: T): void; | ||
} | ||
|
||
declare function walk<T extends ASTNode>(ast: ASTNode, options: WalkOptions<T>): void; | ||
|
||
// Test case 1: Simple type inference | ||
const ast: ASTNode = { | ||
type: 'Declaration', | ||
property: 'color', | ||
value: 'red' | ||
}; | ||
|
||
// This should infer node as Declaration type | ||
walk(ast, { | ||
visit: 'Declaration', | ||
enter(node) { | ||
console.log(node.property); // Should not error - node should be inferred as Declaration | ||
}, | ||
}); | ||
|
||
// Test case 2: More complex scenario | ||
declare const complexAst: Rule; | ||
|
||
walk(complexAst, { | ||
visit: 'Declaration', | ||
enter(node) { | ||
console.log(node.value); // Should infer node as Declaration | ||
}, | ||
}); | ||
|
||
//// [cssTreeTypeInference.js] | ||
// Test case 1: Simple type inference | ||
const ast = { | ||
type: 'Declaration', | ||
property: 'color', | ||
value: 'red' | ||
}; | ||
// This should infer node as Declaration type | ||
walk(ast, { | ||
visit: 'Declaration', | ||
enter(node) { | ||
console.log(node.property); // Should not error - node should be inferred as Declaration | ||
}, | ||
}); | ||
walk(complexAst, { | ||
visit: 'Declaration', | ||
enter(node) { | ||
console.log(node.value); // Should infer node as Declaration | ||
}, | ||
}); |
128 changes: 128 additions & 0 deletions
128
testdata/baselines/reference/compiler/cssTreeTypeInference.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
//// [tests/cases/compiler/cssTreeTypeInference.ts] //// | ||
|
||
=== cssTreeTypeInference.ts === | ||
// Simplified reproduction of css-tree type inference issue | ||
// https://github.com/microsoft/typescript-go/issues/1727 | ||
|
||
interface Declaration { | ||
>Declaration : Symbol(Declaration, Decl(cssTreeTypeInference.ts, 0, 0)) | ||
|
||
type: 'Declaration'; | ||
>type : Symbol(Declaration.type, Decl(cssTreeTypeInference.ts, 3, 23)) | ||
|
||
property: string; | ||
>property : Symbol(Declaration.property, Decl(cssTreeTypeInference.ts, 4, 24)) | ||
|
||
value: string; | ||
>value : Symbol(Declaration.value, Decl(cssTreeTypeInference.ts, 5, 21)) | ||
} | ||
|
||
interface Rule { | ||
>Rule : Symbol(Rule, Decl(cssTreeTypeInference.ts, 7, 1)) | ||
|
||
type: 'Rule'; | ||
>type : Symbol(Rule.type, Decl(cssTreeTypeInference.ts, 9, 16)) | ||
|
||
selector: string; | ||
>selector : Symbol(Rule.selector, Decl(cssTreeTypeInference.ts, 10, 17)) | ||
|
||
children: Declaration[]; | ||
>children : Symbol(Rule.children, Decl(cssTreeTypeInference.ts, 11, 21)) | ||
>Declaration : Symbol(Declaration, Decl(cssTreeTypeInference.ts, 0, 0)) | ||
} | ||
|
||
type ASTNode = Declaration | Rule; | ||
>ASTNode : Symbol(ASTNode, Decl(cssTreeTypeInference.ts, 13, 1)) | ||
>Declaration : Symbol(Declaration, Decl(cssTreeTypeInference.ts, 0, 0)) | ||
>Rule : Symbol(Rule, Decl(cssTreeTypeInference.ts, 7, 1)) | ||
|
||
interface WalkOptions<T extends ASTNode> { | ||
>WalkOptions : Symbol(WalkOptions, Decl(cssTreeTypeInference.ts, 15, 34)) | ||
>T : Symbol(T, Decl(cssTreeTypeInference.ts, 17, 22)) | ||
>ASTNode : Symbol(ASTNode, Decl(cssTreeTypeInference.ts, 13, 1)) | ||
|
||
visit: T['type']; | ||
>visit : Symbol(WalkOptions.visit, Decl(cssTreeTypeInference.ts, 17, 42)) | ||
>T : Symbol(T, Decl(cssTreeTypeInference.ts, 17, 22)) | ||
|
||
enter(node: T): void; | ||
>enter : Symbol(WalkOptions.enter, Decl(cssTreeTypeInference.ts, 18, 21)) | ||
>node : Symbol(node, Decl(cssTreeTypeInference.ts, 19, 10)) | ||
>T : Symbol(T, Decl(cssTreeTypeInference.ts, 17, 22)) | ||
} | ||
|
||
declare function walk<T extends ASTNode>(ast: ASTNode, options: WalkOptions<T>): void; | ||
>walk : Symbol(walk, Decl(cssTreeTypeInference.ts, 20, 1)) | ||
>T : Symbol(T, Decl(cssTreeTypeInference.ts, 22, 22)) | ||
>ASTNode : Symbol(ASTNode, Decl(cssTreeTypeInference.ts, 13, 1)) | ||
>ast : Symbol(ast, Decl(cssTreeTypeInference.ts, 22, 41)) | ||
>ASTNode : Symbol(ASTNode, Decl(cssTreeTypeInference.ts, 13, 1)) | ||
>options : Symbol(options, Decl(cssTreeTypeInference.ts, 22, 54)) | ||
>WalkOptions : Symbol(WalkOptions, Decl(cssTreeTypeInference.ts, 15, 34)) | ||
>T : Symbol(T, Decl(cssTreeTypeInference.ts, 22, 22)) | ||
|
||
// Test case 1: Simple type inference | ||
const ast: ASTNode = { | ||
>ast : Symbol(ast, Decl(cssTreeTypeInference.ts, 25, 5)) | ||
>ASTNode : Symbol(ASTNode, Decl(cssTreeTypeInference.ts, 13, 1)) | ||
|
||
type: 'Declaration', | ||
>type : Symbol(type, Decl(cssTreeTypeInference.ts, 25, 22)) | ||
|
||
property: 'color', | ||
>property : Symbol(property, Decl(cssTreeTypeInference.ts, 26, 24)) | ||
|
||
value: 'red' | ||
>value : Symbol(value, Decl(cssTreeTypeInference.ts, 27, 22)) | ||
|
||
}; | ||
|
||
// This should infer node as Declaration type | ||
walk(ast, { | ||
>walk : Symbol(walk, Decl(cssTreeTypeInference.ts, 20, 1)) | ||
>ast : Symbol(ast, Decl(cssTreeTypeInference.ts, 25, 5)) | ||
|
||
visit: 'Declaration', | ||
>visit : Symbol(visit, Decl(cssTreeTypeInference.ts, 32, 11)) | ||
|
||
enter(node) { | ||
>enter : Symbol(enter, Decl(cssTreeTypeInference.ts, 33, 25)) | ||
>node : Symbol(node, Decl(cssTreeTypeInference.ts, 34, 10)) | ||
|
||
console.log(node.property); // Should not error - node should be inferred as Declaration | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>node.property : Symbol(Declaration.property, Decl(cssTreeTypeInference.ts, 4, 24)) | ||
>node : Symbol(node, Decl(cssTreeTypeInference.ts, 34, 10)) | ||
>property : Symbol(Declaration.property, Decl(cssTreeTypeInference.ts, 4, 24)) | ||
|
||
}, | ||
}); | ||
|
||
// Test case 2: More complex scenario | ||
declare const complexAst: Rule; | ||
>complexAst : Symbol(complexAst, Decl(cssTreeTypeInference.ts, 40, 13)) | ||
>Rule : Symbol(Rule, Decl(cssTreeTypeInference.ts, 7, 1)) | ||
|
||
walk(complexAst, { | ||
>walk : Symbol(walk, Decl(cssTreeTypeInference.ts, 20, 1)) | ||
>complexAst : Symbol(complexAst, Decl(cssTreeTypeInference.ts, 40, 13)) | ||
|
||
visit: 'Declaration', | ||
>visit : Symbol(visit, Decl(cssTreeTypeInference.ts, 42, 18)) | ||
|
||
enter(node) { | ||
>enter : Symbol(enter, Decl(cssTreeTypeInference.ts, 43, 25)) | ||
>node : Symbol(node, Decl(cssTreeTypeInference.ts, 44, 10)) | ||
|
||
console.log(node.value); // Should infer node as Declaration | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>node.value : Symbol(Declaration.value, Decl(cssTreeTypeInference.ts, 5, 21)) | ||
>node : Symbol(node, Decl(cssTreeTypeInference.ts, 44, 10)) | ||
>value : Symbol(Declaration.value, Decl(cssTreeTypeInference.ts, 5, 21)) | ||
|
||
}, | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.