-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve binding element type inference using CheckMode (rebased) #56753
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
weswigham
merged 9 commits into
microsoft:main
from
babakks:improve-binding-element-type-inference-3
Jan 10, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df471c0
Improve binding element type inference
babakks 58db89a
Modify existing test case's '.types' file
babakks 354ebb0
Add tests for array binding pattern assignment
babakks 3f1b524
Add tests for object binding pattern assignment
babakks d00a5b7
Improve test cases
babakks 86a543b
Restrict ignoring circular error to binding element contextual type c…
babakks e872af5
Return `errorType` when resolving binding element contextual type fal…
babakks 6b7d580
Simplify condition on `checkMode`
babakks da1c5a8
Merge branch 'main' into improve-binding-element-type-inference-3
babakks 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
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
48 changes: 48 additions & 0 deletions
48
...s/baselines/reference/destructuringArrayBindingPatternAndAssignment5SiblingInitializer.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,48 @@ | ||
//// [tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts] //// | ||
|
||
//// [destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts] | ||
// To be inferred as `number` | ||
function f1() { | ||
const [a1, b1 = a1] = [1]; | ||
const [a2, b2 = 1 + a2] = [1]; | ||
} | ||
|
||
// To be inferred as `string` | ||
function f2() { | ||
const [a1, b1 = a1] = ['hi']; | ||
const [a2, b2 = a2 + '!'] = ['hi']; | ||
} | ||
|
||
// To be inferred as `string | number` | ||
function f3() { | ||
const [a1, b1 = a1] = ['hi', 1]; | ||
const [a2, b2 = a2 + '!'] = ['hi', 1]; | ||
} | ||
|
||
// Based on comment: | ||
// - https://github.com/microsoft/TypeScript/issues/49989#issuecomment-1852694486 | ||
declare const yadda: [number, number] | undefined | ||
function f4() { | ||
const [ a, b = a ] = yadda ?? []; | ||
} | ||
|
||
|
||
//// [destructuringArrayBindingPatternAndAssignment5SiblingInitializer.js] | ||
// To be inferred as `number` | ||
function f1() { | ||
var _a = [1], a1 = _a[0], _b = _a[1], b1 = _b === void 0 ? a1 : _b; | ||
var _c = [1], a2 = _c[0], _d = _c[1], b2 = _d === void 0 ? 1 + a2 : _d; | ||
} | ||
// To be inferred as `string` | ||
function f2() { | ||
var _a = ['hi'], a1 = _a[0], _b = _a[1], b1 = _b === void 0 ? a1 : _b; | ||
var _c = ['hi'], a2 = _c[0], _d = _c[1], b2 = _d === void 0 ? a2 + '!' : _d; | ||
} | ||
// To be inferred as `string | number` | ||
function f3() { | ||
var _a = ['hi', 1], a1 = _a[0], _b = _a[1], b1 = _b === void 0 ? a1 : _b; | ||
var _c = ['hi', 1], a2 = _c[0], _d = _c[1], b2 = _d === void 0 ? a2 + '!' : _d; | ||
} | ||
function f4() { | ||
var _a = yadda !== null && yadda !== void 0 ? yadda : [], a = _a[0], _b = _a[1], b = _b === void 0 ? a : _b; | ||
} |
63 changes: 63 additions & 0 deletions
63
...elines/reference/destructuringArrayBindingPatternAndAssignment5SiblingInitializer.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,63 @@ | ||
//// [tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts] //// | ||
|
||
=== destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts === | ||
// To be inferred as `number` | ||
function f1() { | ||
>f1 : Symbol(f1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 0, 0)) | ||
|
||
const [a1, b1 = a1] = [1]; | ||
>a1 : Symbol(a1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 2, 11)) | ||
>b1 : Symbol(b1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 2, 14)) | ||
>a1 : Symbol(a1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 2, 11)) | ||
|
||
const [a2, b2 = 1 + a2] = [1]; | ||
>a2 : Symbol(a2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 3, 11)) | ||
>b2 : Symbol(b2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 3, 14)) | ||
>a2 : Symbol(a2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 3, 11)) | ||
} | ||
|
||
// To be inferred as `string` | ||
function f2() { | ||
>f2 : Symbol(f2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 4, 1)) | ||
|
||
const [a1, b1 = a1] = ['hi']; | ||
>a1 : Symbol(a1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 8, 11)) | ||
>b1 : Symbol(b1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 8, 14)) | ||
>a1 : Symbol(a1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 8, 11)) | ||
|
||
const [a2, b2 = a2 + '!'] = ['hi']; | ||
>a2 : Symbol(a2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 9, 11)) | ||
>b2 : Symbol(b2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 9, 14)) | ||
>a2 : Symbol(a2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 9, 11)) | ||
} | ||
|
||
// To be inferred as `string | number` | ||
function f3() { | ||
>f3 : Symbol(f3, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 10, 1)) | ||
|
||
const [a1, b1 = a1] = ['hi', 1]; | ||
>a1 : Symbol(a1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 14, 11)) | ||
>b1 : Symbol(b1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 14, 14)) | ||
>a1 : Symbol(a1, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 14, 11)) | ||
|
||
const [a2, b2 = a2 + '!'] = ['hi', 1]; | ||
>a2 : Symbol(a2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 15, 11)) | ||
>b2 : Symbol(b2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 15, 14)) | ||
>a2 : Symbol(a2, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 15, 11)) | ||
} | ||
|
||
// Based on comment: | ||
// - https://github.com/microsoft/TypeScript/issues/49989#issuecomment-1852694486 | ||
declare const yadda: [number, number] | undefined | ||
>yadda : Symbol(yadda, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 20, 13)) | ||
|
||
function f4() { | ||
>f4 : Symbol(f4, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 20, 49)) | ||
|
||
const [ a, b = a ] = yadda ?? []; | ||
>a : Symbol(a, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 22, 11)) | ||
>b : Symbol(b, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 22, 14)) | ||
>a : Symbol(a, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 22, 11)) | ||
>yadda : Symbol(yadda, Decl(destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts, 20, 13)) | ||
} | ||
|
85 changes: 85 additions & 0 deletions
85
...aselines/reference/destructuringArrayBindingPatternAndAssignment5SiblingInitializer.types
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,85 @@ | ||
//// [tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts] //// | ||
|
||
=== destructuringArrayBindingPatternAndAssignment5SiblingInitializer.ts === | ||
// To be inferred as `number` | ||
function f1() { | ||
>f1 : () => void | ||
|
||
const [a1, b1 = a1] = [1]; | ||
>a1 : number | ||
>b1 : number | ||
>a1 : number | ||
>[1] : [number] | ||
>1 : 1 | ||
|
||
const [a2, b2 = 1 + a2] = [1]; | ||
>a2 : number | ||
>b2 : number | ||
>1 + a2 : number | ||
>1 : 1 | ||
>a2 : number | ||
>[1] : [number] | ||
>1 : 1 | ||
} | ||
|
||
// To be inferred as `string` | ||
function f2() { | ||
>f2 : () => void | ||
|
||
const [a1, b1 = a1] = ['hi']; | ||
>a1 : string | ||
>b1 : string | ||
>a1 : string | ||
>['hi'] : [string] | ||
>'hi' : "hi" | ||
|
||
const [a2, b2 = a2 + '!'] = ['hi']; | ||
>a2 : string | ||
>b2 : string | ||
>a2 + '!' : string | ||
>a2 : string | ||
>'!' : "!" | ||
>['hi'] : [string] | ||
>'hi' : "hi" | ||
} | ||
|
||
// To be inferred as `string | number` | ||
function f3() { | ||
>f3 : () => void | ||
|
||
const [a1, b1 = a1] = ['hi', 1]; | ||
>a1 : string | ||
>b1 : string | number | ||
>a1 : string | ||
>['hi', 1] : [string, number] | ||
>'hi' : "hi" | ||
>1 : 1 | ||
|
||
const [a2, b2 = a2 + '!'] = ['hi', 1]; | ||
>a2 : string | ||
>b2 : string | number | ||
>a2 + '!' : string | ||
>a2 : string | ||
>'!' : "!" | ||
>['hi', 1] : [string, number] | ||
>'hi' : "hi" | ||
>1 : 1 | ||
} | ||
|
||
// Based on comment: | ||
// - https://github.com/microsoft/TypeScript/issues/49989#issuecomment-1852694486 | ||
declare const yadda: [number, number] | undefined | ||
>yadda : [number, number] | ||
|
||
function f4() { | ||
>f4 : () => void | ||
|
||
const [ a, b = a ] = yadda ?? []; | ||
>a : number | ||
>b : number | ||
>a : number | ||
>yadda ?? [] : [number, number] | [] | ||
>yadda : [number, number] | ||
>[] : [] | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
.../baselines/reference/destructuringObjectBindingPatternAndAssignment9SiblingInitializer.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,48 @@ | ||
//// [tests/cases/conformance/es6/destructuring/destructuringObjectBindingPatternAndAssignment9SiblingInitializer.ts] //// | ||
|
||
//// [destructuringObjectBindingPatternAndAssignment9SiblingInitializer.ts] | ||
// To be inferred as `number` | ||
function f1() { | ||
const { a1, b1 = a1 } = { a1: 1 }; | ||
const { a2, b2 = 1 + a2 } = { a2: 1 }; | ||
} | ||
|
||
// To be inferred as `string` | ||
function f2() { | ||
const { a1, b1 = a1 } = { a1: 'hi' }; | ||
const { a2, b2 = a2 + '!' } = { a2: 'hi' }; | ||
} | ||
|
||
// To be inferred as `string | number` | ||
function f3() { | ||
const { a1, b1 = a1 } = { a1: 'hi', b1: 1 }; | ||
const { a2, b2 = a2 + '!' } = { a2: 'hi', b2: 1 }; | ||
} | ||
|
||
// Based on comment: | ||
// - https://github.com/microsoft/TypeScript/issues/49989#issuecomment-1852694486 | ||
declare const yadda: { a?: number, b?: number } | undefined | ||
function f4() { | ||
const { a, b = a } = yadda ?? {}; | ||
} | ||
|
||
|
||
//// [destructuringObjectBindingPatternAndAssignment9SiblingInitializer.js] | ||
// To be inferred as `number` | ||
function f1() { | ||
var _a = { a1: 1 }, a1 = _a.a1, _b = _a.b1, b1 = _b === void 0 ? a1 : _b; | ||
var _c = { a2: 1 }, a2 = _c.a2, _d = _c.b2, b2 = _d === void 0 ? 1 + a2 : _d; | ||
} | ||
// To be inferred as `string` | ||
function f2() { | ||
var _a = { a1: 'hi' }, a1 = _a.a1, _b = _a.b1, b1 = _b === void 0 ? a1 : _b; | ||
var _c = { a2: 'hi' }, a2 = _c.a2, _d = _c.b2, b2 = _d === void 0 ? a2 + '!' : _d; | ||
} | ||
// To be inferred as `string | number` | ||
function f3() { | ||
var _a = { a1: 'hi', b1: 1 }, a1 = _a.a1, _b = _a.b1, b1 = _b === void 0 ? a1 : _b; | ||
var _c = { a2: 'hi', b2: 1 }, a2 = _c.a2, _d = _c.b2, b2 = _d === void 0 ? a2 + '!' : _d; | ||
} | ||
function f4() { | ||
var _a = yadda !== null && yadda !== void 0 ? yadda : {}, a = _a.a, _b = _a.b, b = _b === void 0 ? a : _b; | ||
} |
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.
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.
I think that there is a non-zero chance that this PR will get picked up at some point (cc @jakebailey ). If the team decides to go with it then this should return
errorType
:I think that there is no harm in returning that here already now (unless proven otherwise)
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.
Thanks for the comment. Since, it's an error (of course, not a circularity one but still), I think you're right about changing the return value from
anyType
toerrorType
.Applied in the last commit.
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.
Actually, I might have read this late or smth. I think that
errorType
should be returned if you encounter the circularity error but here you explicitly want to prevent the error from being reported and I think that it should extend to the returned type. So despite my initial reaction, I would recommend returninganyType
here. Sorry for the confusion 🙈