Skip to content

Proposal: Prioritize declaration file resolutions when declaration extension is explicitly specified #58700

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andrewbranch
Copy link
Member

Alternative to #58553
Fixes #58353

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels May 29, 2024
@andrewbranch
Copy link
Member Author

@typescript-bot test top400
@typescript-bot perf test faster

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 29, 2024

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

Command Status Results
test top400 ✅ Started ✅ Results
perf test faster ✅ Started 👀 Results

@typescript-bot
Copy link
Collaborator

@andrewbranch
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 30 30 ~ ~ ~ p=1.000 n=6
Symbols 62,153 62,153 ~ ~ ~ p=1.000 n=6
Types 50,242 50,242 ~ ~ ~ p=1.000 n=6
Memory used 193,532k (± 0.92%) 194,147k (± 0.93%) ~ 192,277k 195,824k p=0.575 n=6
Parse Time 1.31s (± 0.64%) 1.31s (± 0.62%) ~ 1.29s 1.31s p=0.673 n=6
Bind Time 0.72s 0.72s ~ ~ ~ p=1.000 n=6
Check Time 9.55s (± 0.37%) 9.56s (± 0.40%) ~ 9.50s 9.61s p=0.808 n=6
Emit Time 2.63s (± 0.82%) 2.63s (± 0.50%) ~ 2.62s 2.65s p=0.805 n=6
Total Time 14.20s (± 0.33%) 14.22s (± 0.28%) ~ 14.15s 14.26s p=0.574 n=6
angular-1 - node (v18.15.0, x64)
Errors 5 5 ~ ~ ~ p=1.000 n=6
Symbols 944,105 944,105 ~ ~ ~ p=1.000 n=6
Types 407,032 407,032 ~ ~ ~ p=1.000 n=6
Memory used 1,222,119k (± 0.00%) 1,222,136k (± 0.00%) ~ 1,222,090k 1,222,159k p=0.630 n=6
Parse Time 6.80s (± 0.61%) 6.79s (± 0.45%) ~ 6.74s 6.82s p=0.687 n=6
Bind Time 1.88s (± 0.43%) 1.87s (± 0.40%) ~ 1.86s 1.88s p=0.120 n=6
Check Time 31.36s (± 0.33%) 31.28s (± 0.46%) ~ 31.06s 31.48s p=0.336 n=6
Emit Time 14.78s (± 0.33%) 14.85s (± 0.53%) ~ 14.76s 14.99s p=0.126 n=6
Total Time 54.82s (± 0.24%) 54.79s (± 0.35%) ~ 54.54s 55.09s p=0.470 n=6
mui-docs - node (v18.15.0, x64)
Errors 5 5 ~ ~ ~ p=1.000 n=6
Symbols 2,053,011 2,053,011 ~ ~ ~ p=1.000 n=6
Types 899,367 899,367 ~ ~ ~ p=1.000 n=6
Memory used 2,065,912k (± 0.01%) 2,065,814k (± 0.01%) ~ 2,065,635k 2,065,966k p=0.128 n=6
Parse Time 6.82s (± 0.13%) 6.83s (± 0.30%) ~ 6.79s 6.85s p=0.285 n=6
Bind Time 2.30s (± 0.59%) 2.32s (± 1.14%) ~ 2.29s 2.36s p=0.510 n=6
Check Time 69.61s (± 0.40%) 69.07s (± 1.42%) ~ 67.15s 69.78s p=0.298 n=6
Emit Time 0.14s (± 2.88%) 0.14s (± 3.60%) ~ 0.14s 0.15s p=0.595 n=6
Total Time 78.88s (± 0.34%) 78.36s (± 1.23%) ~ 76.48s 79.07s p=0.298 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,226,290 1,226,291 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 260,834 260,834 ~ ~ ~ p=1.000 n=6
Memory used 2,367,745k (± 2.61%) 2,343,518k (± 0.03%) ~ 2,342,710k 2,344,736k p=0.575 n=6
Parse Time 4.94s (± 0.74%) 4.97s (± 1.12%) ~ 4.89s 5.05s p=0.575 n=6
Bind Time 1.91s (± 0.80%) 1.90s (± 0.21%) ~ 1.90s 1.91s p=0.863 n=6
Check Time 33.83s (± 0.35%) 33.82s (± 0.44%) ~ 33.63s 34.01s p=0.688 n=6
Emit Time 2.58s (± 4.23%) 2.55s (± 3.31%) ~ 2.44s 2.66s p=0.810 n=6
Total Time 43.26s (± 0.54%) 43.27s (± 0.34%) ~ 43.13s 43.47s p=0.936 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,226,290 1,226,291 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 260,834 260,834 ~ ~ ~ p=1.000 n=6
Memory used 2,520,489k (± 3.11%) 2,470,581k (± 3.15%) ~ 2,419,154k 2,571,406k p=0.471 n=6
Parse Time 7.71s (± 1.12%) 7.62s (± 0.65%) ~ 7.53s 7.67s p=0.066 n=6
Bind Time 2.50s (± 0.25%) 2.51s (± 0.60%) ~ 2.48s 2.52s p=0.295 n=6
Check Time 49.86s (± 0.39%) 49.95s (± 0.65%) ~ 49.68s 50.54s p=0.810 n=6
Emit Time 3.83s (± 4.08%) 3.79s (± 3.26%) ~ 3.69s 3.97s p=0.936 n=6
Total Time 63.90s (± 0.48%) 63.89s (± 0.65%) ~ 63.51s 64.63s p=0.689 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 257,977 257,978 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 104,898 104,898 ~ ~ ~ p=1.000 n=6
Memory used 426,874k (± 0.01%) 426,991k (± 0.01%) +117k (+ 0.03%) 426,929k 427,084k p=0.005 n=6
Parse Time 3.32s (± 0.78%) 3.34s (± 0.88%) ~ 3.31s 3.39s p=0.332 n=6
Bind Time 1.31s (± 1.15%) 1.32s (± 0.31%) ~ 1.31s 1.32s p=0.730 n=6
Check Time 18.01s (± 0.32%) 17.98s (± 0.36%) ~ 17.87s 18.06s p=0.336 n=6
Emit Time 1.35s (± 1.11%) 1.32s (± 1.63%) -0.03s (- 2.22%) 1.29s 1.35s p=0.029 n=6
Total Time 23.99s (± 0.27%) 23.96s (± 0.36%) ~ 23.85s 24.06s p=0.572 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 35 35 ~ ~ ~ p=1.000 n=6
Symbols 224,565 224,565 ~ ~ ~ p=1.000 n=6
Types 93,734 93,734 ~ ~ ~ p=1.000 n=6
Memory used 369,787k (± 0.02%) 369,824k (± 0.02%) ~ 369,725k 369,924k p=0.471 n=6
Parse Time 2.83s (± 0.96%) 2.84s (± 0.50%) ~ 2.82s 2.86s p=0.746 n=6
Bind Time 1.59s (± 0.92%) 1.59s (± 1.04%) ~ 1.56s 1.60s p=0.511 n=6
Check Time 15.69s (± 0.24%) 15.69s (± 0.17%) ~ 15.65s 15.72s p=0.935 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.12s (± 0.11%) 20.12s (± 0.17%) ~ 20.07s 20.17s p=1.000 n=6
vscode - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,840,700 2,840,700 ~ ~ ~ p=1.000 n=6
Types 962,692 962,692 ~ ~ ~ p=1.000 n=6
Memory used 3,013,970k (± 0.00%) 3,013,960k (± 0.00%) ~ 3,013,922k 3,014,011k p=1.000 n=6
Parse Time 13.83s (± 0.38%) 13.84s (± 0.24%) ~ 13.80s 13.90s p=0.571 n=6
Bind Time 4.18s (± 2.16%) 4.32s (± 2.94%) +0.14s (+ 3.35%) 4.16s 4.48s p=0.017 n=6
Check Time 74.69s (± 2.42%) 73.36s (± 0.47%) ~ 72.86s 73.86s p=0.109 n=6
Emit Time 22.32s (± 8.83%) 23.48s (± 0.57%) ~ 23.30s 23.66s p=1.000 n=6
Total Time 115.02s (± 0.28%) 115.00s (± 0.37%) ~ 114.36s 115.47s p=0.936 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 266,048 266,048 ~ ~ ~ p=1.000 n=6
Types 108,470 108,470 ~ ~ ~ p=1.000 n=6
Memory used 410,917k (± 0.02%) 410,915k (± 0.01%) ~ 410,865k 410,953k p=0.575 n=6
Parse Time 4.76s (± 0.84%) 4.79s (± 0.56%) ~ 4.76s 4.82s p=0.195 n=6
Bind Time 2.08s (± 0.83%) 2.08s (± 0.90%) ~ 2.05s 2.10s p=0.808 n=6
Check Time 21.07s (± 0.47%) 21.09s (± 0.28%) ~ 21.02s 21.17s p=0.810 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 27.90s (± 0.49%) 27.96s (± 0.26%) ~ 27.87s 28.09s p=1.000 n=6
xstate-main - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 535,743 535,743 ~ ~ ~ p=1.000 n=6
Types 182,738 182,738 ~ ~ ~ p=1.000 n=6
Memory used 470,456k (± 0.02%) 470,417k (± 0.01%) ~ 470,355k 470,464k p=0.689 n=6
Parse Time 3.94s (± 0.49%) 3.96s (± 0.40%) ~ 3.94s 3.98s p=0.105 n=6
Bind Time 1.49s (± 0.81%) 1.49s (± 1.18%) ~ 1.46s 1.51s p=0.932 n=6
Check Time 22.60s (± 0.34%) 22.59s (± 0.26%) ~ 22.50s 22.65s p=0.873 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 28.03s (± 0.30%) 28.04s (± 0.21%) ~ 27.97s 28.11s p=0.810 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

@andrewbranch Here are the results of running the top 400 repos comparing main and refs/pull/58700/merge:

Everything looks good!

@robpalme
Copy link

Does this mean that any DTS file importing any relative specifier that ends with *.d.ts will always attempt a non-searching explicit resolution before trying out other options? (Because that sounds great)

@andrewbranch
Copy link
Member Author

Yep, exactly. Could you explain how this would be useful for you? The motivation for this in #58353 is pretty narrow and specific.

@robpalme
Copy link

robpalme commented May 31, 2024

Could you explain how this would be useful for you?

It's not for immediate use. And it's not something I'd pitch in isolation. But I'm happy to be a supporting voice.

As a general rule, explicit/predictable/simple resolution behavior helps the long-term direction of making TypeScript more flexible/adaptable to custom JS environments. The opposite direction would be for TS have unavoidable implicit behavior, e.g. to bake-in popular conventions such as assuming some kind of extension-searching order, or assuming existence of a package.json, or assuming that dependencies live in a directory called node_modules. Whilst it's appropriate/important/helpful to support mainstream implicit assumptions with almost zero-config, I'd say it is also important to permit escape hatches where advanced users can override them. Escape hatches like this that don't require a flag and don't upset mainstream usage are pure gold.

So this is just one instance of that wider choice. Explicit *.d.ts resolution means we have the option of authoring DTS files in a resilient manner. We already use a custom pipeline for emitting/transforming DTS files, packaging them up, and unpacking dependencies in an ever-growing global dependency cache directory containing immutable per-package per-version declarations. This explicit behavior gives us an escape hatch if we decide to start shipping source/arbitrary files in there too.

@robpalme
Copy link

robpalme commented Jun 5, 2024

As a follow up, we just had a slightly more concrete instance of a situation where explicit resolution would have reduced confusion.

As part of a migration to enable verbatimModuleSyntax, we found an error where an import was relying on index-resolution, even though our runtime does not support that.

// main.ts
import { myType } from "../dir"   // runtime error only when VMS enabled
// main.d.ts (generated)
import { myType } from "../dir"
// dir/index.ts
export type myType = string;

This worked before verbatimModuleSyntax because TypeScript erased the runtime import so this mismatch between compile-time and runtime resolution was never exposed. In the short-term we'll fix this by creating a type-only import that imports the file using an explicit path.

// main.ts
import type { myType } from "../dir/index"

We'd like to have a way to fix this confusion long-term by preventing index-resolution of types both in source and in DTS files via explicit extensions. That ensures that both humans and computers will not get confused about the meaning of an import. And may allow us to switch to a more appropriate TS resolution mode in future.

// main.ts
import type { myType } from "../dir/index.ts"
// main.d.ts (generated)
import type { myType } from "../dir/index.d.ts"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When relatively importing a .d.ts file in a declaration file, TypeScript loads a .ts file instead
3 participants