Improve contextual types inferred from return types#61913
Improve contextual types inferred from return types#61913ahejlsberg wants to merge 3 commits intomainfrom
Conversation
There was a problem hiding this comment.
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 them on 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 |
|
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 |
| 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, |
|
Minimal repro case for the type Many<T> = T | readonly T[];
declare function pick<T extends object, U extends keyof T>(
object: T,
...props: Array<Many<U>>
): Pick<T, U>;
type ProductLandingContextT = {
title: string;
intro: string;
};
const getProductLandingContextFromRequest = async (
req: any,
): Promise<ProductLandingContextT> => {
const page = req.context.page;
const title = await page.renderProp("title", req.context, { textOnly: true });
return {
title,
...pick(page, ["intro"]),
};
}; |
|
@typescript-bot pack this |
|
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your and then running There is also a playground for this build and an npm module you can use via |
Implements the experiment discussed here.