- Notifications
You must be signed in to change notification settings - Fork12.9k
Improve contextual types inferred from return types#61913
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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.
Pull Request Overview
This PR improves contextual type inference by introducing a new inference priority for mapping return types.
- Added a new enum member ReturnMapper and updated the MaxValue and combination mask in InferencePriority.
- Modified the type-checker to exclude any/unknown types when using the ReturnMapper inference and passed the new flag into the inferTypes function.
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/compiler/types.ts | Added ReturnMapper to the InferencePriority enum and updated related bit masks. |
src/compiler/checker.ts | Updated inference filtering to exclude any/unknown types and passed the ReturnMapper flag to inferTypes. |
Comments suppressed due to low confidence (1)
Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document themon our wiki's API Breaking Changes page. Also, please make sure@DanielRosenwasser and@RyanCavanaugh are aware of the changes, just as a heads up. |
@typescript-bot test it |
typescript-bot commentedJun 22, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey@ahejlsberg, the results of running the DT tests are ready. Everything looks the same! |
@ahejlsberg Here are the results of running the user tests with tsc comparing Everything looks good! |
@ahejlsberg Here they are:tscComparison Report - baseline..pr
System info unknown Hosts
Scenarios
Developer Information: |
@ahejlsberg Here are the results of running the top 400 repos with tsc comparing Something interesting changed - please have a look. Details
|
This PR addresses a different situation, but its generic title reminded me of#58910 ;p |
@@ -7129,6 +7130,7 @@ export const enum InferenceFlags { | |||
NoDefault = 1 << 0, // Infer silentNeverType for no inferences (otherwise anyType or unknownType) | |||
AnyDefault = 1 << 1, // Infer anyType (in JS files) for no inferences (otherwise unknownType) | |||
SkippedGenericFunction = 1 << 2, // A generic function was skipped during inference | |||
NoUnknownInference = 1 << 3, |
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.
nit: this stays unused
Minimal repro case for the typeMany<T>=T|readonlyT[];declarefunctionpick<Textendsobject,UextendskeyofT>(object:T, ...props:Array<Many<U>>):Pick<T,U>;typeProductLandingContextT={title:string;intro:string;};constgetProductLandingContextFromRequest=async(req:any,):Promise<ProductLandingContextT>=>{constpage=req.context.page;consttitle=awaitpage.renderProp("title",req.context,{textOnly:true});return{ title, ...pick(page,["intro"]),};}; |
@typescript-bot pack this |
typescript-bot commentedJun 23, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
typescript-bot commentedJun 23, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey@jakebailey, I've packed this intoan installable tgz. You can install it for testing by referencing it in your
and then running There is also a playgroundfor this build and annpm module you can use via |
Implements the experiment discussedhere.