-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Infer from context sensitive return expressions #60909
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
Andarist
wants to merge
10
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/inference-from-context-sensitive-return
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
65b54ad
Infer from context sensitive return expressions
Andarist 7ccc000
update baselines
Andarist 37af847
fmt
Andarist 4aa5ca7
stop redundant inference from returns of non-context sensitive functions
Andarist 3095dad
update baselines
Andarist c671b45
add `CheckMode.SkipReturnTypeFromBodyInference`
Andarist c61ea39
split tests
Andarist 23c6c33
add more tests
Andarist 20c1a37
Merge remote-tracking branch 'origin/main' into fix/inference-from-co…
Andarist a14bad4
Merge branch 'main' into fix/inference-from-context-sensitive-return
RyanCavanaugh 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
295 changes: 295 additions & 0 deletions
295
tests/baselines/reference/InferFromReturnsInContextSensitive1.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,295 @@ | ||
//// [tests/cases/conformance/types/typeRelationships/typeInference/InferFromReturnsInContextSensitive1.ts] //// | ||
|
||
//// [InferFromReturnsInContextSensitive1.ts] | ||
// https://github.com/microsoft/TypeScript/issues/60720 | ||
|
||
type Options<TContext> = { | ||
onStart?: () => TContext; | ||
onEnd?: (context: TContext) => void; | ||
}; | ||
|
||
function create<TContext>(builder: (arg: boolean) => Options<TContext>) { | ||
return builder(true); | ||
} | ||
|
||
create((arg) => ({ | ||
onStart: () => ({ time: new Date() }), | ||
onEnd: (context) => {}, | ||
})); | ||
|
||
create((arg) => ({ | ||
onEnd: (context) => {}, | ||
onStart: () => ({ time: new Date() }), | ||
})); | ||
|
||
// https://github.com/microsoft/TypeScript/issues/57021 | ||
|
||
type Schema = Record<string, unknown>; | ||
|
||
type StepFunction<TSchema extends Schema = Schema> = (anything: unknown) => { | ||
readonly schema: TSchema; | ||
readonly toAnswers?: (keys: keyof TSchema) => unknown; | ||
}; | ||
|
||
function step<TSchema extends Schema = Schema>( | ||
stepVal: StepFunction<TSchema>, | ||
): StepFunction<TSchema> { | ||
return stepVal; | ||
} | ||
|
||
const stepResult1 = step((_something) => ({ | ||
schema: { | ||
attribute: "anything", | ||
}, | ||
toAnswers: (keys) => { | ||
type Test = string extends typeof keys ? never : "true"; | ||
const test: Test = "true"; // ok | ||
return { test }; | ||
}, | ||
})); | ||
|
||
const stepResult2 = step((_something) => ({ | ||
toAnswers: (keys) => { | ||
type Test = string extends typeof keys ? never : "true"; | ||
const test: Test = "true"; // ok | ||
return { test }; | ||
}, | ||
schema: { | ||
attribute: "anything", | ||
}, | ||
})); | ||
|
||
type Fn1<T, T2> = (anything: unknown) => { | ||
stuff: T; | ||
consume: (arg: T) => (anything: unknown) => { | ||
stuff2: T2; | ||
consume2: (arg: T2) => void; | ||
}; | ||
}; | ||
|
||
declare function test1<T, T2>(fn: Fn1<T, T2>): [T, T2]; | ||
|
||
const res1 = test1((_something) => ({ | ||
stuff: "foo", | ||
consume: (arg) => { | ||
return (_something) => ({ | ||
stuff2: 42, | ||
consume2: (arg2) => {}, | ||
}); | ||
}, | ||
})); | ||
|
||
const res2 = test1((_something) => ({ | ||
consume: (arg) => { | ||
return (_something) => ({ | ||
consume2: (arg2) => {}, | ||
stuff2: 42, | ||
}); | ||
}, | ||
stuff: "foo", | ||
})); | ||
|
||
const res3 = test1((_something) => ({ | ||
stuff: "foo", | ||
consume: () => { | ||
return (_something) => ({ | ||
stuff2: 42, | ||
consume2: (arg2) => {}, | ||
}); | ||
}, | ||
})); | ||
|
||
const res4 = test1((_something) => ({ | ||
consume: () => { | ||
return (_something) => ({ | ||
consume2: (arg2) => {}, | ||
stuff2: 42, | ||
}); | ||
}, | ||
stuff: "foo", | ||
})); | ||
|
||
const res5 = test1((_something) => ({ | ||
stuff: "foo", | ||
consume: () => { | ||
return () => ({ | ||
stuff2: 42, | ||
consume2: (arg2) => {}, | ||
}); | ||
}, | ||
})); | ||
|
||
const res6 = test1((_something) => ({ | ||
consume: () => { | ||
return () => ({ | ||
consume2: (arg2) => {}, | ||
stuff2: 42, | ||
}); | ||
}, | ||
stuff: "foo", | ||
})); | ||
|
||
const res7 = test1((_something) => ({ | ||
stuff: "foo", | ||
consume: () => { | ||
return () => ({ | ||
stuff2: 42, | ||
consume2: () => {}, | ||
}); | ||
}, | ||
})); | ||
|
||
const res8 = test1((_something) => ({ | ||
consume: () => { | ||
return () => ({ | ||
consume2: () => {}, | ||
stuff2: 42, | ||
}); | ||
}, | ||
stuff: "foo", | ||
})); | ||
|
||
|
||
//// [InferFromReturnsInContextSensitive1.js] | ||
"use strict"; | ||
// https://github.com/microsoft/TypeScript/issues/60720 | ||
function create(builder) { | ||
return builder(true); | ||
} | ||
create(function (arg) { return ({ | ||
onStart: function () { return ({ time: new Date() }); }, | ||
onEnd: function (context) { }, | ||
}); }); | ||
create(function (arg) { return ({ | ||
onEnd: function (context) { }, | ||
onStart: function () { return ({ time: new Date() }); }, | ||
}); }); | ||
function step(stepVal) { | ||
return stepVal; | ||
} | ||
var stepResult1 = step(function (_something) { return ({ | ||
schema: { | ||
attribute: "anything", | ||
}, | ||
toAnswers: function (keys) { | ||
var test = "true"; // ok | ||
return { test: test }; | ||
}, | ||
}); }); | ||
var stepResult2 = step(function (_something) { return ({ | ||
toAnswers: function (keys) { | ||
var test = "true"; // ok | ||
return { test: test }; | ||
}, | ||
schema: { | ||
attribute: "anything", | ||
}, | ||
}); }); | ||
var res1 = test1(function (_something) { return ({ | ||
stuff: "foo", | ||
consume: function (arg) { | ||
return function (_something) { return ({ | ||
stuff2: 42, | ||
consume2: function (arg2) { }, | ||
}); }; | ||
}, | ||
}); }); | ||
var res2 = test1(function (_something) { return ({ | ||
consume: function (arg) { | ||
return function (_something) { return ({ | ||
consume2: function (arg2) { }, | ||
stuff2: 42, | ||
}); }; | ||
}, | ||
stuff: "foo", | ||
}); }); | ||
var res3 = test1(function (_something) { return ({ | ||
stuff: "foo", | ||
consume: function () { | ||
return function (_something) { return ({ | ||
stuff2: 42, | ||
consume2: function (arg2) { }, | ||
}); }; | ||
}, | ||
}); }); | ||
var res4 = test1(function (_something) { return ({ | ||
consume: function () { | ||
return function (_something) { return ({ | ||
consume2: function (arg2) { }, | ||
stuff2: 42, | ||
}); }; | ||
}, | ||
stuff: "foo", | ||
}); }); | ||
var res5 = test1(function (_something) { return ({ | ||
stuff: "foo", | ||
consume: function () { | ||
return function () { return ({ | ||
stuff2: 42, | ||
consume2: function (arg2) { }, | ||
}); }; | ||
}, | ||
}); }); | ||
var res6 = test1(function (_something) { return ({ | ||
consume: function () { | ||
return function () { return ({ | ||
consume2: function (arg2) { }, | ||
stuff2: 42, | ||
}); }; | ||
}, | ||
stuff: "foo", | ||
}); }); | ||
var res7 = test1(function (_something) { return ({ | ||
stuff: "foo", | ||
consume: function () { | ||
return function () { return ({ | ||
stuff2: 42, | ||
consume2: function () { }, | ||
}); }; | ||
}, | ||
}); }); | ||
var res8 = test1(function (_something) { return ({ | ||
consume: function () { | ||
return function () { return ({ | ||
consume2: function () { }, | ||
stuff2: 42, | ||
}); }; | ||
}, | ||
stuff: "foo", | ||
}); }); | ||
|
||
|
||
//// [InferFromReturnsInContextSensitive1.d.ts] | ||
type Options<TContext> = { | ||
onStart?: () => TContext; | ||
onEnd?: (context: TContext) => void; | ||
}; | ||
declare function create<TContext>(builder: (arg: boolean) => Options<TContext>): Options<TContext>; | ||
type Schema = Record<string, unknown>; | ||
type StepFunction<TSchema extends Schema = Schema> = (anything: unknown) => { | ||
readonly schema: TSchema; | ||
readonly toAnswers?: (keys: keyof TSchema) => unknown; | ||
}; | ||
declare function step<TSchema extends Schema = Schema>(stepVal: StepFunction<TSchema>): StepFunction<TSchema>; | ||
declare const stepResult1: StepFunction<{ | ||
attribute: string; | ||
}>; | ||
declare const stepResult2: StepFunction<{ | ||
attribute: string; | ||
}>; | ||
type Fn1<T, T2> = (anything: unknown) => { | ||
stuff: T; | ||
consume: (arg: T) => (anything: unknown) => { | ||
stuff2: T2; | ||
consume2: (arg: T2) => void; | ||
}; | ||
}; | ||
declare function test1<T, T2>(fn: Fn1<T, T2>): [T, T2]; | ||
declare const res1: [string, number]; | ||
declare const res2: [string, number]; | ||
declare const res3: [string, number]; | ||
declare const res4: [string, number]; | ||
declare const res5: [string, number]; | ||
declare const res6: [string, number]; | ||
declare const res7: [string, number]; | ||
declare const res8: [string, number]; |
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.
It's separate from
SkipGenericFunctions
becausegetReturnTypeFromBody
removesSkipGenericFunctions
bit - so when the compiler gets to a function node with type parameters within a context-sensitive function's return it can't just useSkipGenericFunctions
to skip over inferring its own return type from body.The goal of
SkipReturnTypeFromBodyInference
is to persist throughgetReturnTypeFromBody
.