Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ahejlsberg
Copy link
Member

Implements the experiment discussed here.

@Copilot Copilot AI review requested due to automatic review settings June 22, 2025 20:10
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Jun 22, 2025
@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jun 22, 2025
Copy link

@Copilot Copilot AI left a 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)

@typescript-bot
Copy link
Collaborator

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.

@ahejlsberg
Copy link
Member Author

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 22, 2025

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started 👀 Results
user test this ✅ Started ✅ Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

@typescript-bot
Copy link
Collaborator

Hey @ahejlsberg, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user tests with tsc comparing main and refs/pull/61913/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

@ahejlsberg
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 34 34 ~ ~ ~ p=1.000 n=6
Symbols 62,370 62,370 ~ ~ ~ p=1.000 n=6
Types 50,386 50,386 ~ ~ ~ p=1.000 n=6
Memory used 194,094k (± 0.95%) 193,548k (± 0.77%) ~ 192,815k 196,567k p=0.378 n=6
Parse Time 1.31s (± 0.31%) 1.30s (± 0.75%) ~ 1.29s 1.31s p=0.213 n=6
Bind Time 0.73s 0.73s ~ ~ ~ p=1.000 n=6
Check Time 9.69s (± 0.29%) 9.66s (± 0.22%) ~ 9.63s 9.69s p=0.077 n=6
Emit Time 2.74s (± 0.93%) 2.73s (± 1.40%) ~ 2.67s 2.77s p=1.000 n=6
Total Time 14.46s (± 0.17%) 14.42s (± 0.33%) ~ 14.37s 14.49s p=0.169 n=6
angular-1 - node (v18.15.0, x64)
Errors 56 56 ~ ~ ~ p=1.000 n=6
Symbols 948,750 948,576 -174 (- 0.02%) ~ ~ p=0.001 n=6
Types 410,846 410,804 -42 (- 0.01%) ~ ~ p=0.001 n=6
Memory used 1,224,589k (± 0.01%) 1,224,440k (± 0.00%) -149k (- 0.01%) 1,224,395k 1,224,531k p=0.008 n=6
Parse Time 6.62s (± 0.50%) 6.64s (± 0.98%) ~ 6.55s 6.70s p=0.377 n=6
Bind Time 1.89s (± 0.93%) 1.89s (± 0.33%) ~ 1.88s 1.90s p=1.000 n=6
Check Time 31.83s (± 0.14%) 31.79s (± 0.27%) ~ 31.65s 31.88s p=0.470 n=6
Emit Time 14.87s (± 0.57%) 14.90s (± 0.42%) ~ 14.84s 14.99s p=0.520 n=6
Total Time 55.20s (± 0.12%) 55.22s (± 0.21%) ~ 55.02s 55.33s p=0.575 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,561,658 2,561,485 -173 (- 0.01%) ~ ~ p=0.001 n=6
Types 892,631 892,606 -25 (- 0.00%) ~ ~ p=0.001 n=6
Memory used 2,836,579k (± 0.00%) 2,836,448k (± 0.00%) -130k (- 0.00%) 2,836,398k 2,836,529k p=0.013 n=6
Parse Time 9.20s (± 0.50%) 9.18s (± 0.25%) ~ 9.14s 9.20s p=0.571 n=6
Bind Time 2.30s (± 0.22%) 2.31s (± 0.33%) ~ 2.30s 2.32s p=0.247 n=6
Check Time 83.62s (± 0.32%) 83.67s (± 0.64%) ~ 82.96s 84.42s p=0.873 n=6
Emit Time 0.30s (± 1.37%) 0.30s (± 1.37%) ~ 0.29s 0.30s p=1.000 n=6
Total Time 95.41s (± 0.29%) 95.45s (± 0.58%) ~ 94.73s 96.21s p=0.810 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,227,126 1,227,160 +34 (+ 0.00%) ~ ~ p=0.001 n=6
Types 267,408 267,426 +18 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 2,362,056k (± 0.02%) 2,422,890k (± 6.15%) ~ 2,361,445k 2,727,187k p=0.689 n=6
Parse Time 5.20s (± 0.72%) 5.20s (± 1.00%) ~ 5.10s 5.25s p=0.747 n=6
Bind Time 1.80s (± 0.29%) 1.80s (± 0.42%) ~ 1.79s 1.81s p=0.784 n=6
Check Time 35.12s (± 0.19%) 35.05s (± 0.44%) ~ 34.76s 35.19s p=0.377 n=6
Emit Time 3.03s (± 4.41%) 2.96s (± 1.02%) ~ 2.93s 3.02s p=0.298 n=6
Total Time 45.16s (± 0.35%) 45.03s (± 0.30%) ~ 44.82s 45.17s p=0.230 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,227,126 1,227,160 +34 (+ 0.00%) ~ ~ p=0.001 n=6
Types 267,408 267,426 +18 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 2,700,732k (±13.34%) 2,796,386k (±14.25%) ~ 2,431,816k 3,162,628k p=0.575 n=6
Parse Time 6.85s (± 1.83%) 6.86s (± 1.24%) ~ 6.77s 6.95s p=0.689 n=6
Bind Time 2.20s (± 2.23%) 2.20s (± 1.37%) ~ 2.16s 2.25s p=1.000 n=6
Check Time 42.60s (± 1.09%) 42.62s (± 0.59%) ~ 42.40s 42.94s p=0.630 n=6
Emit Time 3.61s (± 3.93%) 3.46s (± 1.38%) 🟩-0.15s (- 4.06%) 3.41s 3.55s p=0.045 n=6
Total Time 55.25s (± 0.98%) 55.12s (± 0.55%) ~ 54.83s 55.46s p=0.810 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 262,474 262,470 -4 (- 0.00%) ~ ~ p=0.001 n=6
Types 107,135 107,127 -8 (- 0.01%) ~ ~ p=0.001 n=6
Memory used 441,755k (± 0.01%) 441,712k (± 0.01%) ~ 441,640k 441,809k p=0.230 n=6
Parse Time 3.51s (± 1.70%) 3.55s (± 1.31%) ~ 3.50s 3.60s p=0.126 n=6
Bind Time 1.31s (± 1.25%) 1.32s (± 0.83%) ~ 1.31s 1.34s p=0.188 n=6
Check Time 18.83s (± 0.45%) 18.82s (± 0.23%) ~ 18.77s 18.88s p=1.000 n=6
Emit Time 1.52s (± 0.77%) 1.51s (± 0.91%) ~ 1.50s 1.54s p=0.564 n=6
Total Time 25.17s (± 0.49%) 25.20s (± 0.28%) ~ 25.11s 25.29s p=0.296 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 71 71 ~ ~ ~ p=1.000 n=6
Symbols 225,327 225,354 +27 (+ 0.01%) ~ ~ p=0.001 n=6
Types 94,288 94,289 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 371,169k (± 0.03%) 371,112k (± 0.02%) ~ 371,048k 371,239k p=0.378 n=6
Parse Time 2.90s (± 1.17%) 2.88s (± 0.90%) ~ 2.84s 2.91s p=0.418 n=6
Bind Time 1.60s (± 0.61%) 1.61s (± 2.00%) ~ 1.56s 1.64s p=0.373 n=6
Check Time 16.36s (± 0.28%) 16.26s (± 0.31%) -0.10s (- 0.61%) 16.20s 16.35s p=0.020 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.86s (± 0.33%) 20.75s (± 0.43%) ~ 20.65s 20.88s p=0.092 n=6
vscode - node (v18.15.0, x64)
Errors 33 33 ~ ~ ~ p=1.000 n=6
Symbols 3,478,623 3,478,628 +5 (+ 0.00%) ~ ~ p=0.001 n=6
Types 1,170,082 1,170,057 -25 (- 0.00%) ~ ~ p=0.001 n=6
Memory used 3,526,701k (± 0.01%) 3,526,781k (± 0.01%) ~ 3,526,343k 3,527,039k p=0.378 n=6
Parse Time 15.06s (± 0.59%) 15.08s (± 0.53%) ~ 14.98s 15.16s p=0.629 n=6
Bind Time 4.87s (± 0.53%) 4.85s (± 0.38%) ~ 4.83s 4.88s p=0.223 n=6
Check Time 98.25s (± 3.23%) 96.25s (± 3.35%) ~ 93.12s 100.63s p=0.298 n=6
Emit Time 30.39s (± 2.82%) 30.37s (± 4.16%) ~ 29.34s 32.54s p=0.936 n=6
Total Time 148.57s (± 2.63%) 146.57s (± 2.88%) ~ 142.59s 151.35s p=0.470 n=6
webpack - node (v18.15.0, x64)
Errors 2 2 ~ ~ ~ p=1.000 n=6
Symbols 320,000 320,000 ~ ~ ~ p=1.000 n=6
Types 140,356 140,355 -1 (- 0.00%) ~ ~ p=0.001 n=6
Memory used 474,123k (± 0.01%) 474,190k (± 0.02%) ~ 474,041k 474,306k p=0.199 n=6
Parse Time 4.33s (± 0.71%) 4.33s (± 0.79%) ~ 4.28s 4.37s p=0.936 n=6
Bind Time 1.77s (± 1.21%) 1.78s (± 1.17%) ~ 1.75s 1.80s p=0.871 n=6
Check Time 20.67s (± 0.29%) 20.66s (± 0.55%) ~ 20.57s 20.85s p=0.686 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 26.76s (± 0.26%) 26.77s (± 0.36%) ~ 26.68s 26.93s p=1.000 n=6
xstate-main - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 667,374 667,148 -226 (- 0.03%) ~ ~ p=0.001 n=6
Types 198,751 198,664 -87 (- 0.04%) ~ ~ p=0.001 n=6
Memory used 570,150k (± 0.00%) 570,009k (± 0.02%) ~ 569,915k 570,225k p=0.066 n=6
Parse Time 5.54s (± 0.56%) 5.51s (± 1.13%) ~ 5.43s 5.57s p=0.809 n=6
Bind Time 1.65s (± 0.54%) 1.66s (± 0.38%) ~ 1.65s 1.67s p=0.070 n=6
Check Time 25.33s (± 0.28%) 25.25s (± 1.43%) ~ 24.52s 25.49s p=0.520 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 32.51s (± 0.17%) 32.42s (± 1.08%) ~ 31.73s 32.69s p=0.809 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the top 400 repos with tsc comparing main and refs/pull/61913/merge:

Something interesting changed - please have a look.

Details

github/docs

1 of 2 projects failed to build with the old tsc and were ignored

tsconfig.json

  • error TS2739: Type '{ productVideo: any; productVideoTranscript: any; hasGuidesPage: any; product: { href: any; title: any; }; whatsNewChangelog: any; changelogUrl: any; productCommunityExamples: any; ghesReleases: any; ... 6 more ...; shortTitle: any; }' is missing the following properties from type 'ProductLandingContextT': introPlainText, intro, beta_product

gvergnaud/ts-pattern

tests/tsconfig.json

@Andarist
Copy link
Contributor

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this stays unused

@Andarist
Copy link
Contributor

Minimal repro case for the github/docs problem:

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"]),
  };
};

@jakebailey
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 23, 2025

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started ✅ Results

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 23, 2025

Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/165448/artifacts?artifactName=tgz&fileId=BEE85749DC012DFE0439FCB8B8A57B4EE5CFE5CE56B343DD2C6737B725BE3D7C02&fileName=/typescript-5.9.0-insiders.20250623.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

4 participants