-
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 type predicates from function bodies using control flow analysis #57465
Merged
RyanCavanaugh
merged 43 commits into
microsoft:main
from
danvk:infer-type-predicate-16069
Mar 15, 2024
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
0c24ccc
Infer type predicates from function bodies
danvk e2684f1
Run formatter
danvk 101df93
Add secondary subtype check and tests
danvk d0e385e
add union type test to baselines
danvk a72b1f1
add prisma circularity test with failing baseline
danvk ef2d465
Various fixes for circularity issue
danvk 9336052
circularity test is fixed
danvk 41f624d
revert back to CheckMode.TypeOnly
danvk 52df115
Add test case for a predicate that throws
danvk 3ab6fae
Drop isTriviallyNonBoolean, switch to simpler test, check for assertions
danvk 9591231
Use unescapeLeadingUnderscores
danvk 9a8c0a1
tests are fixed
danvk a4ff6b4
Always bind flow nodes to return statements + other fixes
ahejlsberg 869422f
Accept new baselines
ahejlsberg adbdc7d
Merge branch 'suggested-changes-57465' into infer-type-predicate-16069
danvk 0dec9c6
simplify
danvk 703253a
Delay expensive functionHasImplicitReturn call
ahejlsberg c7f1c3d
Avoid creating closures
ahejlsberg 25743a3
Add fallback isTypeAssignableTo check to test for equivalence
danvk 4e79d76
try caching the antecedent
danvk a5725d2
revert fallback assignability check
danvk 3323573
Revert "try caching the antecedent"
danvk 76a5abd
Merge commit 'c7f1c3d309' into infer-type-predicate-16069
danvk 5ec6f1f
accept baselines
danvk 3491c78
ignore rest parameters & update baseline
danvk c4ee1f0
switch to getParameterCount
danvk 4e934fa
Revert "switch to getParameterCount"
danvk 37951ca
Revert "ignore rest parameters & update baseline"
danvk cabed97
Revert "accept baselines"
danvk 6a88111
try setting @declaration: true
danvk 151d026
accept baselines
danvk 0b6a24a
Merge branch 'main' into test-fix-expt-16069
danvk 0f31362
accept baseline
danvk 2354072
Add rest parameter test and accept baselines
danvk f63105c
ignore rest parameters & update baseline
danvk eea3994
switch to getParameterCount
danvk e474bfc
accept baseline
danvk a6a734c
emit inferred type predicates in createReturnTypeOfSignatureDeclaration
danvk f5a9404
accept baselines
danvk 784e2a3
correct isDate / flakyIsDate pair
danvk 33826df
handle asserts predicates as well
danvk f002b28
Pass flags for type predicate emit
danvk 50803a0
Factor out nodeBuilder.typePredicateToTypePredicateNode
danvk 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
40 changes: 40 additions & 0 deletions
40
tests/baselines/reference/circularConstructorWithReturn.js
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,40 @@ | ||
//// [tests/cases/compiler/circularConstructorWithReturn.ts] //// | ||
|
||
//// [circularConstructorWithReturn.ts] | ||
// This should not be a circularity error. See | ||
// https://github.com/microsoft/TypeScript/pull/57465#issuecomment-1960271216 | ||
export type Client = ReturnType<typeof getPrismaClient> extends new () => infer T ? T : never | ||
|
||
export function getPrismaClient(options?: any) { | ||
class PrismaClient { | ||
self: Client; | ||
constructor(options?: any) { | ||
return (this.self = applyModelsAndClientExtensions(this)); | ||
} | ||
} | ||
|
||
return PrismaClient | ||
} | ||
|
||
export function applyModelsAndClientExtensions(client: Client) { | ||
return client; | ||
} | ||
|
||
|
||
//// [circularConstructorWithReturn.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getPrismaClient = getPrismaClient; | ||
exports.applyModelsAndClientExtensions = applyModelsAndClientExtensions; | ||
function getPrismaClient(options) { | ||
var PrismaClient = /** @class */ (function () { | ||
function PrismaClient(options) { | ||
return (this.self = applyModelsAndClientExtensions(this)); | ||
} | ||
return PrismaClient; | ||
}()); | ||
return PrismaClient; | ||
} | ||
function applyModelsAndClientExtensions(client) { | ||
return client; | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/baselines/reference/circularConstructorWithReturn.symbols
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,48 @@ | ||
//// [tests/cases/compiler/circularConstructorWithReturn.ts] //// | ||
|
||
=== circularConstructorWithReturn.ts === | ||
// This should not be a circularity error. See | ||
// https://github.com/microsoft/TypeScript/pull/57465#issuecomment-1960271216 | ||
export type Client = ReturnType<typeof getPrismaClient> extends new () => infer T ? T : never | ||
>Client : Symbol(Client, Decl(circularConstructorWithReturn.ts, 0, 0)) | ||
>ReturnType : Symbol(ReturnType, Decl(lib.es5.d.ts, --, --)) | ||
>getPrismaClient : Symbol(getPrismaClient, Decl(circularConstructorWithReturn.ts, 2, 93)) | ||
>T : Symbol(T, Decl(circularConstructorWithReturn.ts, 2, 79)) | ||
>T : Symbol(T, Decl(circularConstructorWithReturn.ts, 2, 79)) | ||
|
||
export function getPrismaClient(options?: any) { | ||
>getPrismaClient : Symbol(getPrismaClient, Decl(circularConstructorWithReturn.ts, 2, 93)) | ||
>options : Symbol(options, Decl(circularConstructorWithReturn.ts, 4, 32)) | ||
|
||
class PrismaClient { | ||
>PrismaClient : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48)) | ||
|
||
self: Client; | ||
>self : Symbol(PrismaClient.self, Decl(circularConstructorWithReturn.ts, 5, 22)) | ||
>Client : Symbol(Client, Decl(circularConstructorWithReturn.ts, 0, 0)) | ||
|
||
constructor(options?: any) { | ||
>options : Symbol(options, Decl(circularConstructorWithReturn.ts, 7, 16)) | ||
|
||
return (this.self = applyModelsAndClientExtensions(this)); | ||
>this.self : Symbol(PrismaClient.self, Decl(circularConstructorWithReturn.ts, 5, 22)) | ||
>this : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48)) | ||
>self : Symbol(PrismaClient.self, Decl(circularConstructorWithReturn.ts, 5, 22)) | ||
>applyModelsAndClientExtensions : Symbol(applyModelsAndClientExtensions, Decl(circularConstructorWithReturn.ts, 13, 1)) | ||
>this : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48)) | ||
} | ||
} | ||
|
||
return PrismaClient | ||
>PrismaClient : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48)) | ||
} | ||
|
||
export function applyModelsAndClientExtensions(client: Client) { | ||
>applyModelsAndClientExtensions : Symbol(applyModelsAndClientExtensions, Decl(circularConstructorWithReturn.ts, 13, 1)) | ||
>client : Symbol(client, Decl(circularConstructorWithReturn.ts, 15, 47)) | ||
>Client : Symbol(Client, Decl(circularConstructorWithReturn.ts, 0, 0)) | ||
|
||
return client; | ||
>client : Symbol(client, Decl(circularConstructorWithReturn.ts, 15, 47)) | ||
} | ||
|
46 changes: 46 additions & 0 deletions
46
tests/baselines/reference/circularConstructorWithReturn.types
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,46 @@ | ||
//// [tests/cases/compiler/circularConstructorWithReturn.ts] //// | ||
|
||
=== circularConstructorWithReturn.ts === | ||
// This should not be a circularity error. See | ||
// https://github.com/microsoft/TypeScript/pull/57465#issuecomment-1960271216 | ||
export type Client = ReturnType<typeof getPrismaClient> extends new () => infer T ? T : never | ||
>Client : PrismaClient | ||
>getPrismaClient : (options?: any) => typeof PrismaClient | ||
|
||
export function getPrismaClient(options?: any) { | ||
>getPrismaClient : (options?: any) => typeof PrismaClient | ||
>options : any | ||
|
||
class PrismaClient { | ||
>PrismaClient : PrismaClient | ||
|
||
self: Client; | ||
>self : PrismaClient | ||
|
||
constructor(options?: any) { | ||
>options : any | ||
|
||
return (this.self = applyModelsAndClientExtensions(this)); | ||
>(this.self = applyModelsAndClientExtensions(this)) : PrismaClient | ||
>this.self = applyModelsAndClientExtensions(this) : PrismaClient | ||
>this.self : PrismaClient | ||
>this : this | ||
>self : PrismaClient | ||
>applyModelsAndClientExtensions(this) : PrismaClient | ||
>applyModelsAndClientExtensions : (client: PrismaClient) => PrismaClient | ||
>this : this | ||
} | ||
} | ||
|
||
return PrismaClient | ||
>PrismaClient : typeof PrismaClient | ||
} | ||
|
||
export function applyModelsAndClientExtensions(client: Client) { | ||
>applyModelsAndClientExtensions : (client: Client) => PrismaClient | ||
>client : PrismaClient | ||
|
||
return client; | ||
>client : PrismaClient | ||
} | ||
|
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.
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.
Slight change here: previously this did not call
setEmitFlags(factory.createIdentifier(typePredicate.parameterName), EmitFlags.NoAsciiEscaping)
here but now it does vianodeBuilder.typePredicateToTypePredicateNode
. I wasn't sure if this difference was intentional. Making these consistent doesn't break any tests, but I can model the difference if we want to keep it.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.
AFAIK
NoAsciiEscaping
doesn't do anything for things that aren't string literals, so I'm honestly not sure why it's here.