-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 context sensitive return expressions #60909
Open
Andarist
wants to merge
8
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 1 commit
Commits
Show all changes
8 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 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
145 changes: 145 additions & 0 deletions
145
tests/baselines/reference/intraExpressionInferencesInContextSensitive1.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,145 @@ | ||
//// [tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferencesInContextSensitive1.ts] //// | ||
|
||
//// [intraExpressionInferencesInContextSensitive1.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: boolean) => ({ | ||
onStart: () => ({ time: new Date() }), | ||
onEnd: (context) => {}, | ||
})); | ||
|
||
create(() => ({ | ||
onStart: () => ({ time: new Date() }), | ||
onEnd: (context) => {}, | ||
})); | ||
|
||
create((arg) => ({ | ||
onStart: () => ({ time: new Date() }), | ||
onEnd: (context) => {}, | ||
})); | ||
|
||
// 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 step1<TSchema extends Schema = Schema>( | ||
stepVal: StepFunction<TSchema>, | ||
): StepFunction<TSchema> { | ||
return stepVal; | ||
} | ||
|
||
const stepResult1 = step1((_something) => ({ | ||
schema: { | ||
attribute: "anything", | ||
}, | ||
toAnswers: (keys) => { | ||
type Test = string extends typeof keys ? never : "true"; | ||
const test: Test = "true"; // ok | ||
return { test }; | ||
}, | ||
})); | ||
|
||
type StepFunction2<TSchema extends Schema = Schema> = (anything: unknown) => { | ||
readonly schema: (thing: number) => TSchema; | ||
readonly toAnswers?: (keys: keyof TSchema) => unknown; | ||
}; | ||
|
||
function step2<TSchema extends Schema = Schema>( | ||
stepVal: StepFunction2<TSchema>, | ||
): StepFunction2<TSchema> { | ||
return stepVal; | ||
} | ||
|
||
const stepResult2 = step2((_something) => ({ | ||
schema: (thing) => ({ | ||
attribute: "anything", | ||
}), | ||
toAnswers: (keys) => { | ||
type Test = string extends typeof keys ? never : "true"; | ||
const test: Test = "true"; // ok | ||
return { test }; | ||
}, | ||
})); | ||
|
||
|
||
//// [intraExpressionInferencesInContextSensitive1.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 () { return ({ | ||
onStart: function () { return ({ time: new Date() }); }, | ||
onEnd: function (context) { }, | ||
}); }); | ||
create(function (arg) { return ({ | ||
onStart: function () { return ({ time: new Date() }); }, | ||
onEnd: function (context) { }, | ||
}); }); | ||
function step1(stepVal) { | ||
return stepVal; | ||
} | ||
var stepResult1 = step1(function (_something) { return ({ | ||
schema: { | ||
attribute: "anything", | ||
}, | ||
toAnswers: function (keys) { | ||
var test = "true"; // ok | ||
return { test: test }; | ||
}, | ||
}); }); | ||
function step2(stepVal) { | ||
return stepVal; | ||
} | ||
var stepResult2 = step2(function (_something) { return ({ | ||
schema: function (thing) { return ({ | ||
attribute: "anything", | ||
}); }, | ||
toAnswers: function (keys) { | ||
var test = "true"; // ok | ||
return { test: test }; | ||
}, | ||
}); }); | ||
|
||
|
||
//// [intraExpressionInferencesInContextSensitive1.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 step1<TSchema extends Schema = Schema>(stepVal: StepFunction<TSchema>): StepFunction<TSchema>; | ||
declare const stepResult1: StepFunction<{ | ||
attribute: string; | ||
}>; | ||
type StepFunction2<TSchema extends Schema = Schema> = (anything: unknown) => { | ||
readonly schema: (thing: number) => TSchema; | ||
readonly toAnswers?: (keys: keyof TSchema) => unknown; | ||
}; | ||
declare function step2<TSchema extends Schema = Schema>(stepVal: StepFunction2<TSchema>): StepFunction2<TSchema>; | ||
declare const stepResult2: StepFunction2<{ | ||
attribute: string; | ||
}>; |
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.
I literally wrote this code in August so I need to take another look at this, self-review it and add more tests. In the meantime though, it could be helpful for me to learn what the extended test suite thinks about this. cc @jakebailey :p
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.
Hm, damn self-check :p I need to fix this first: repro