-
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
Infer from usage by similarity to builtin types #33263
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3be6e75
Improve names in infer-from-usage
sandersn 2a8ee1f
Merge branch 'master' into infer-from-usage/similarity-to-builtins
sandersn 0f215fd
Merge branch 'master' into infer-from-usage/similarity-to-builtins
sandersn d347b08
Copied from old branch
sandersn c93f919
Merge branch 'master' into infer-from-usage/similarity-to-builtins
sandersn 945d423
Fix bugs in combineUsages/getSignatureFromCalls
sandersn 37150d9
Turn on findBuiltinTypes
sandersn 383286f
Add type parameter inference
sandersn 17d1a7e
Merge branch 'master' into infer-from-usage/similarity-to-builtins
sandersn ff38a1b
Merge branch 'master' into infer-from-usage/similarity-to-builtins
sandersn 052a3d9
Infer void from expr statement usage, not calls
sandersn d32c6b2
Fallback type is always any now
sandersn f394190
Tonnes of cleanup
sandersn 1703ae0
Renames and more cleanup
sandersn 330e51f
Add test + reshuffle/rename new code
sandersn b69f5af
Merge branch 'master' into infer-from-usage/similarity-to-builtins
sandersn 3c79225
Update baselines with any[] inferences
sandersn de7d68a
Even more renaming
sandersn 84e857b
use forEachEntry
sandersn 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,9 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function inferAny( [| app |] ) { | ||
//// const result = app.use('hi') | ||
//// return result | ||
//// } | ||
|
||
verify.rangeAfterCodeFix("app: { use: (arg0: string) => any; }"); |
This file contains 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,8 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function inferVoid( [| app |] ) { | ||
//// app.use('hi') | ||
//// } | ||
|
||
verify.rangeAfterCodeFix("app: { use: (arg0: string) => void; }"); |
This file contains 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 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,10 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function wrap( [| s |] ) { | ||
//// return s.length + s.indexOf('hi') | ||
//// } | ||
|
||
// https://github.com/Microsoft/TypeScript/issues/29330 | ||
verify.rangeAfterCodeFix("s: string | string[]"); | ||
|
This file contains 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,9 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function foo([|a, m |]) { | ||
//// return a + m | ||
//// } | ||
|
||
verify.rangeAfterCodeFix("a: any, m: any", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); | ||
|
This file contains 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,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function foo([|p, a, b, c, d, e |]) { | ||
//// var x: string = a.pop() | ||
//// b.reverse() | ||
//// var rr: boolean[] = c.reverse() | ||
//// d.some(t => t > 1); // can't infer from callbacks right now | ||
//// var y = e.concat(12); // can't infer from overloaded functions right now | ||
//// return p.push(12) | ||
//// } | ||
|
||
verify.rangeAfterCodeFix("p: number[], a: string[], b: any[], c: boolean[], d: any[], e: any[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); | ||
|
This file contains 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 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 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
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixInferFromUsageLiteralTypes.ts
This file contains 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,10 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function foo([|a, m |]) { | ||
//// a = 'hi' | ||
//// m = 1 | ||
//// } | ||
|
||
verify.rangeAfterCodeFix("a: string, m: number", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); | ||
|
This file contains 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,9 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function foo([|p |]) { | ||
//// return p.then((x: string[]) => x[0]) | ||
//// } | ||
|
||
verify.rangeAfterCodeFix("p: Promise<string[]>", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); | ||
|
This file contains 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 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 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,12 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @noImplicitAny: true | ||
//// function foo([|p, a, b |]) { | ||
//// var x | ||
//// p.charAt(x) | ||
//// a.charAt(0) | ||
//// b.concat('hi') | ||
//// } | ||
|
||
verify.rangeAfterCodeFix("p: string, a: string, b: string | any[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0); | ||
|
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.
Even
@internal
, some people are going to be ecstatic that this finally found its way into our API surface and will therefore finally be callable by people using the API (even if via cast).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.
Uhhhh should I be worried about exposing too much? I feeling like assignability is a State Secret or something.
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'm really interested in the reason why it should be marked as
@internal
. Don't get me wrong, I'm one of those people ecstatic that this found it's way to the API, now I can ditch my custom typescript version from my library which exposed this. I'm just super curious about the reasoning behind this decision :)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.
Effectively this function now has to cross conceptual boundaries in TypeScript. Going from being useful internally only inside checker to being used inside the TSServices, which meant it needed to be exposed as a public API on the checker internally within our codebase,
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.
Ok, but why the
@internal
annotation? It looks like the method is still kinda private?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'm not 100% convinced that arbitrary uses of this function are safe and won't corrupt compiler state. Marking it
@internal
allows me to use it inside Typescript where I know how it's being used. Using it outside is anany
cast away, but that way it's obvious that it's not technically supported.