Skip to content

Support resolving @typescript/[lib] in node modules - #45771

Merged
orta merged 8 commits into
microsoft:mainfrom
orta:libTS
Sep 15, 2021
Merged

Support resolving @typescript/[lib] in node modules #45771
orta merged 8 commits into
microsoft:mainfrom
orta:libTS

Conversation

@orta

@ortaorta commented Sep 7, 2021

Copy link
Copy Markdown
Contributor

Note that #46046 updated this implementation with adding @typescript/lib-[x].


Closes#45518 as an alternative which leaves the responsibilities of configuring the environment to your package manager. Re #45688#45685 and #44795

This answer to supporting your own versions of in-built libraries uses the node_modules resolution structure to let you define libraries. The path lookup looks like:

  • lib.dom.d.ts -> @typescript/dom
  • lib.dom.iterable.d.ts -> @typescript/dom/iterable
  • lib.es2015.symbol.wellknown.d.ts -> @typescript/es2015/symbol-wellknown

If you made a package which just sets up @typescript/es2015/symbol-wellknown.d.ts without @typescript/es2015/index.d.ts, then es2015 would still resolve to the TypeScript hosted version.

All JS dependency managers support this syntax:

{
"devDependencies": {
"@typescript/lib-dom": "npm:@types/web"
}
}

This gives people the chance to make their own package with the .d.ts files in the right place and then set it up safely in their own project similar to @types. As we own @typescript/x on npm (and only have a few modules like @typescript/vfs, none of which clash) - then we don't need to worry about 'what happens if someone accidentally includes it twice' via the dependency tree.

@typescript-bottypescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 7, 2021
@orta

orta commented Sep 14, 2021

Copy link
Copy Markdown
ContributorAuthor

@typescript-bot perf test
@typescript-bot pack this

@typescript-bot

typescript-bot commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

Heya @orta, I've started to run the perf test suite on this PR at daa99ce. You can monitor the build here.

Update: The results are in!

@typescript-bot

typescript-bot commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

Heya @orta, I've started to run the tarball bundle task on this PR at daa99ce. You can monitor the build here.

@typescript-bot

typescript-bot commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

Hey @orta, 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/110462/artifacts?artifactName=tgz&fileId=2DE81E5CC943F129DD37FCDD4A05363F2460D8444D5A74006585CA7254DA912A02&fileName=/typescript-4.5.0-insiders.20210914.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/pr-build@4.5.0-pr-45771-3".;

@typescript-bot

Copy link
Copy Markdown
Contributor

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

Here they are:

Comparison Report - main..45771

Metricmain45771DeltaBestWorst
Angular - node (v10.16.3, x64)
Memory used351,531k (± 0.02%)351,585k (± 0.02%)+54k (+ 0.02%)351,465k351,753k
Parse Time1.90s (± 0.30%)1.92s (± 0.56%)+0.02s (+ 1.00%)1.90s1.94s
Bind Time0.85s (± 0.80%)0.85s (± 1.09%)-0.00s (- 0.23%)0.84s0.88s
Check Time5.43s (± 0.42%)5.43s (± 0.37%)+0.00s (+ 0.00%)5.39s5.48s
Emit Time5.81s (± 0.50%)5.85s (± 0.51%)+0.03s (+ 0.53%)5.79s5.94s
Total Time14.00s (± 0.27%)14.04s (± 0.34%)+0.05s (+ 0.35%)13.94s14.19s
Compiler-Unions - node (v10.16.3, x64)
Memory used203,538k (± 0.04%)203,558k (± 0.02%)+19k (+ 0.01%)203,416k203,639k
Parse Time0.78s (± 0.87%)0.79s (± 0.94%)+0.01s (+ 0.64%)0.77s0.80s
Bind Time0.52s (± 1.11%)0.53s (± 1.44%)+0.00s (+ 0.57%)0.51s0.54s
Check Time7.88s (± 0.61%)7.88s (± 0.60%)-0.01s (- 0.08%)7.76s8.01s
Emit Time2.45s (± 0.69%)2.48s (± 1.15%)+0.03s (+ 1.27%)2.43s2.56s
Total Time11.64s (± 0.53%)11.67s (± 0.52%)+0.04s (+ 0.30%)11.49s11.83s
Monaco - node (v10.16.3, x64)
Memory used340,597k (± 0.02%)340,642k (± 0.03%)+44k (+ 0.01%)340,364k340,803k
Parse Time1.44s (± 0.99%)1.46s (± 0.61%)+0.02s (+ 1.04%)1.44s1.48s
Bind Time0.75s (± 1.04%)0.75s (± 0.82%)+0.00s (+ 0.00%)0.74s0.76s
Check Time5.45s (± 0.48%)5.46s (± 0.35%)+0.01s (+ 0.20%)5.42s5.50s
Emit Time3.17s (± 0.87%)3.16s (± 0.83%)-0.00s (- 0.09%)3.11s3.23s
Total Time10.80s (± 0.44%)10.82s (± 0.30%)+0.02s (+ 0.18%)10.74s10.88s
TFS - node (v10.16.3, x64)
Memory used304,064k (± 0.02%)304,081k (± 0.02%)+17k (+ 0.01%)303,892k304,208k
Parse Time1.18s (± 0.34%)1.19s (± 0.68%)+0.01s (+ 0.76%)1.17s1.20s
Bind Time0.71s (± 1.05%)0.72s (± 0.69%)+0.00s (+ 0.42%)0.70s0.72s
Check Time4.95s (± 0.66%)4.95s (± 0.57%)+0.00s (+ 0.06%)4.88s5.00s
Emit Time3.31s (± 0.95%)3.37s (± 1.73%)+0.06s (+ 1.84%)3.28s3.51s
Total Time10.15s (± 0.51%)10.22s (± 0.82%)+0.08s (+ 0.75%)10.08s10.42s
material-ui - node (v10.16.3, x64)
Memory used468,283k (± 0.02%)468,332k (± 0.01%)+49k (+ 0.01%)468,231k468,407k
Parse Time1.73s (± 0.22%)1.75s (± 0.47%)+0.02s (+ 1.28%)1.73s1.76s
Bind Time0.67s (± 1.12%)0.67s (± 0.97%)+0.00s (+ 0.15%)0.65s0.68s
Check Time14.29s (± 0.32%)14.34s (± 0.72%)+0.05s (+ 0.38%)14.08s14.59s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time16.68s (± 0.25%)16.76s (± 0.58%)+0.07s (+ 0.43%)16.52s16.98s
Angular - node (v12.1.0, x64)
Memory used329,505k (± 0.02%)329,556k (± 0.03%)+51k (+ 0.02%)329,424k329,852k
Parse Time1.87s (± 0.61%)1.90s (± 0.34%)+0.03s (+ 1.60%)1.89s1.92s
Bind Time0.84s (± 0.59%)0.84s (± 0.79%)+0.00s (+ 0.48%)0.83s0.86s
Check Time5.29s (± 0.55%)5.28s (± 0.50%)-0.01s (- 0.17%)5.22s5.33s
Emit Time6.10s (± 0.70%)6.11s (± 0.98%)+0.01s (+ 0.20%)6.02s6.32s
Total Time14.10s (± 0.40%)14.13s (± 0.60%)+0.03s (+ 0.21%)13.99s14.40s
Compiler-Unions - node (v12.1.0, x64)
Memory used191,055k (± 0.12%)191,080k (± 0.07%)+25k (+ 0.01%)190,554k191,257k
Parse Time0.78s (± 0.51%)0.78s (± 0.97%)+0.01s (+ 0.77%)0.77s0.80s
Bind Time0.53s (± 1.09%)0.53s (± 0.89%)+0.00s (+ 0.57%)0.52s0.54s
Check Time7.43s (± 0.56%)7.44s (± 0.58%)+0.01s (+ 0.07%)7.36s7.56s
Emit Time2.46s (± 0.51%)2.47s (± 0.74%)+0.01s (+ 0.49%)2.43s2.51s
Total Time11.20s (± 0.44%)11.23s (± 0.48%)+0.03s (+ 0.23%)11.10s11.38s
Monaco - node (v12.1.0, x64)
Memory used323,687k (± 0.08%)323,766k (± 0.05%)+79k (+ 0.02%)323,136k323,949k
Parse Time1.44s (± 0.78%)1.44s (± 1.02%)+0.00s (+ 0.28%)1.41s1.47s
Bind Time0.73s (± 1.18%)0.73s (± 0.84%)-0.00s (- 0.14%)0.72s0.75s
Check Time5.33s (± 0.60%)5.32s (± 0.41%)-0.00s (- 0.04%)5.29s5.37s
Emit Time3.22s (± 0.97%)3.22s (± 0.53%)-0.00s (- 0.12%)3.18s3.25s
Total Time10.72s (± 0.58%)10.71s (± 0.30%)-0.00s (- 0.02%)10.65s10.81s
TFS - node (v12.1.0, x64)
Memory used288,824k (± 0.03%)288,819k (± 0.02%)-5k (- 0.00%)288,661k288,941k
Parse Time1.21s (± 0.51%)1.22s (± 0.77%)+0.01s (+ 0.50%)1.20s1.24s
Bind Time0.70s (± 0.42%)0.70s (± 1.71%)+0.00s (+ 0.43%)0.68s0.73s
Check Time4.90s (± 0.56%)4.90s (± 0.42%)-0.00s (- 0.06%)4.86s4.95s
Emit Time3.39s (± 1.11%)3.40s (± 0.55%)+0.01s (+ 0.27%)3.36s3.45s
Total Time10.21s (± 0.52%)10.23s (± 0.37%)+0.02s (+ 0.18%)10.14s10.31s
material-ui - node (v12.1.0, x64)
Memory used447,022k (± 0.05%)447,014k (± 0.07%)-8k (- 0.00%)445,824k447,300k
Parse Time1.73s (± 0.67%)1.75s (± 0.50%)+0.02s (+ 1.22%)1.73s1.76s
Bind Time0.65s (± 0.89%)0.65s (± 0.77%)-0.01s (- 1.23%)0.63s0.65s
Check Time12.96s (± 1.00%)13.06s (± 0.71%)+0.10s (+ 0.74%)12.81s13.23s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time15.34s (± 0.84%)15.45s (± 0.65%)+0.11s (+ 0.71%)15.19s15.64s
Angular - node (v14.15.1, x64)
Memory used328,057k (± 0.01%)328,079k (± 0.01%)+21k (+ 0.01%)328,034k328,141k
Parse Time1.90s (± 0.63%)1.93s (± 0.60%)+0.02s (+ 1.26%)1.91s1.96s
Bind Time0.88s (± 0.65%)0.88s (± 0.66%)+0.00s (+ 0.34%)0.87s0.90s
Check Time5.31s (± 0.55%)5.34s (± 0.30%)+0.03s (+ 0.56%)5.31s5.39s
Emit Time6.22s (± 0.46%)6.21s (± 0.70%)-0.02s (- 0.29%)6.13s6.34s
Total Time14.32s (± 0.27%)14.36s (± 0.31%)+0.04s (+ 0.29%)14.30s14.51s
Compiler-Unions - node (v14.15.1, x64)
Memory used191,905k (± 0.56%)191,597k (± 0.60%)-308k (- 0.16%)189,629k192,945k
Parse Time0.80s (± 0.62%)0.81s (± 0.64%)+0.01s (+ 0.75%)0.80s0.83s
Bind Time0.56s (± 1.00%)0.56s (± 0.40%)+0.00s (+ 0.18%)0.55s0.56s
Check Time7.55s (± 0.66%)7.56s (± 0.54%)+0.01s (+ 0.11%)7.49s7.64s
Emit Time2.44s (± 1.03%)2.45s (± 0.67%)+0.01s (+ 0.45%)2.42s2.49s
Total Time11.36s (± 0.56%)11.39s (± 0.43%)+0.03s (+ 0.24%)11.28s11.51s
Monaco - node (v14.15.1, x64)
Memory used322,563k (± 0.00%)322,566k (± 0.00%)+3k (+ 0.00%)322,543k322,589k
Parse Time1.49s (± 0.68%)1.49s (± 0.86%)+0.01s (+ 0.40%)1.47s1.53s
Bind Time0.75s (± 0.40%)0.75s (± 0.69%)-0.00s (- 0.13%)0.74s0.76s
Check Time5.26s (± 0.53%)5.27s (± 0.55%)+0.01s (+ 0.13%)5.21s5.34s
Emit Time3.24s (± 0.73%)3.23s (± 0.77%)-0.00s (- 0.09%)3.19s3.29s
Total Time10.73s (± 0.38%)10.74s (± 0.40%)+0.01s (+ 0.13%)10.65s10.83s
TFS - node (v14.15.1, x64)
Memory used287,738k (± 0.00%)287,753k (± 0.01%)+15k (+ 0.01%)287,720k287,787k
Parse Time1.23s (± 1.87%)1.23s (± 1.79%)+0.00s (+ 0.08%)1.20s1.29s
Bind Time0.74s (± 3.03%)0.76s (± 4.64%)+0.02s (+ 3.13%)0.72s0.84s
Check Time4.91s (± 0.38%)4.90s (± 0.44%)-0.01s (- 0.31%)4.85s4.95s
Emit Time3.48s (± 0.49%)3.47s (± 0.58%)-0.02s (- 0.46%)3.43s3.52s
Total Time10.36s (± 0.24%)10.36s (± 0.55%)-0.00s (- 0.01%)10.23s10.47s
material-ui - node (v14.15.1, x64)
Memory used445,503k (± 0.00%)445,507k (± 0.00%)+4k (+ 0.00%)445,487k445,542k
Parse Time1.77s (± 0.57%)1.78s (± 0.37%)+0.01s (+ 0.85%)1.76s1.79s
Bind Time0.69s (± 0.64%)0.70s (± 0.68%)+0.01s (+ 0.72%)0.69s0.71s
Check Time13.05s (± 0.56%)13.08s (± 0.46%)+0.03s (+ 0.24%)12.96s13.19s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time15.51s (± 0.52%)15.56s (± 0.39%)+0.05s (+ 0.32%)15.43s15.67s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory9 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v10.16.3, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • material-ui - node (v14.15.1, x64)
BenchmarkNameIterations
Current4577110
Baselinemain10

Developer Information:

Download Benchmark

@orta

orta commented Sep 14, 2021

Copy link
Copy Markdown
ContributorAuthor

Actually trying this IRL is giving me unexpected results,

Code:
var localOverride = ts.resolveModuleName("@typescript/" + path, currentDirectory, { moduleResolution: ts.ModuleResolutionKind.NodeJs }, host);
console.log({ libFileName, localOverride, currentDirectory })
Result:
{
libFileName: 'lib.dom.d.ts',
localOverride: {
resolvedModule: undefined,
failedLookupLocations: [
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.ts',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.tsx',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.d.ts',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.ts',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.tsx',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.d.ts',
'/Users/ortatherox/dev/typescript/repros/node_modules/@types/typescript__dom/package.json',
'/Users/ortatherox/dev/typescript/repros/node_modules/@types/typescript__dom.d.ts',
'/Users/ortatherox/dev/typescript/repros/node_modules/@types/typescript__dom/index.d.ts',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.ts',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.tsx',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.d.ts',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.ts',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.tsx',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.d.ts',
'/Users/ortatherox/dev/typescript/node_modules/@types/typescript__dom/package.json',
'/Users/ortatherox/dev/typescript/node_modules/@types/typescript__dom.d.ts',
'/Users/ortatherox/dev/typescript/node_modules/@types/typescript__dom/index.d.ts',
'/Users/ortatherox/dev/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/dev/node_modules/@typescript/dom.ts',
'/Users/ortatherox/dev/node_modules/@typescript/dom.tsx',
'/Users/ortatherox/dev/node_modules/@typescript/dom.d.ts',
'/Users/ortatherox/dev/node_modules/@typescript/dom/index.ts',
'/Users/ortatherox/dev/node_modules/@typescript/dom/index.tsx',
'/Users/ortatherox/dev/node_modules/@typescript/dom/index.d.ts',
'/Users/ortatherox/dev/node_modules/@types/typescript__dom/package.json',
'/Users/ortatherox/dev/node_modules/@types/typescript__dom.d.ts',
'/Users/ortatherox/dev/node_modules/@types/typescript__dom/index.d.ts',
'/Users/ortatherox/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/node_modules/@typescript/dom.ts',
'/Users/ortatherox/node_modules/@typescript/dom.tsx',
'/Users/ortatherox/node_modules/@typescript/dom.d.ts',
'/Users/ortatherox/node_modules/@typescript/dom/index.ts',
'/Users/ortatherox/node_modules/@typescript/dom/index.tsx',
'/Users/ortatherox/node_modules/@typescript/dom/index.d.ts',
'/Users/ortatherox/node_modules/@types/typescript__dom/package.json',
'/Users/ortatherox/node_modules/@types/typescript__dom.d.ts',
'/Users/ortatherox/node_modules/@types/typescript__dom/index.d.ts',
'/Users/node_modules/@typescript/dom/package.json',
'/Users/node_modules/@typescript/dom.ts',
'/Users/node_modules/@typescript/dom.tsx',
'/Users/node_modules/@typescript/dom.d.ts',
'/Users/node_modules/@typescript/dom/index.ts',
'/Users/node_modules/@typescript/dom/index.tsx',
'/Users/node_modules/@typescript/dom/index.d.ts',
'/Users/node_modules/@types/typescript__dom/package.json',
'/Users/node_modules/@types/typescript__dom.d.ts',
'/Users/node_modules/@types/typescript__dom/index.d.ts',
'/node_modules/@typescript/dom/package.json',
'/node_modules/@typescript/dom.ts',
'/node_modules/@typescript/dom.tsx',
'/node_modules/@typescript/dom.d.ts',
'/node_modules/@typescript/dom/index.ts',
'/node_modules/@typescript/dom/index.tsx',
'/node_modules/@typescript/dom/index.d.ts',
'/node_modules/@types/typescript__dom/package.json',
'/node_modules/@types/typescript__dom.d.ts',
'/node_modules/@types/typescript__dom/index.d.ts',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.js',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom.jsx',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.js',
'/Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.jsx',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.js',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom.jsx',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.js',
'/Users/ortatherox/dev/typescript/node_modules/@typescript/dom/index.jsx',
'/Users/ortatherox/dev/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/dev/node_modules/@typescript/dom.js',
'/Users/ortatherox/dev/node_modules/@typescript/dom.jsx',
'/Users/ortatherox/dev/node_modules/@typescript/dom/index.js',
'/Users/ortatherox/dev/node_modules/@typescript/dom/index.jsx',
'/Users/ortatherox/node_modules/@typescript/dom/package.json',
'/Users/ortatherox/node_modules/@typescript/dom.js',
'/Users/ortatherox/node_modules/@typescript/dom.jsx',
'/Users/ortatherox/node_modules/@typescript/dom/index.js',
'/Users/ortatherox/node_modules/@typescript/dom/index.jsx',
'/Users/node_modules/@typescript/dom/package.json',
'/Users/node_modules/@typescript/dom.js',
'/Users/node_modules/@typescript/dom.jsx',
'/Users/node_modules/@typescript/dom/index.js',
'/Users/node_modules/@typescript/dom/index.jsx',
'/node_modules/@typescript/dom/package.json',
'/node_modules/@typescript/dom.js',
'/node_modules/@typescript/dom.jsx',
'/node_modules/@typescript/dom/index.js',
'/node_modules/@typescript/dom/index.jsx'
]
},
currentDirectory: '/Users/ortatherox/dev/typescript/repros/tsLibOverrides'
}

What I want: /Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom/index.d.ts
Closest I get: /Users/ortatherox/dev/typescript/repros/node_modules/@typescript/dom/index.d.ts

Perhaps instead of currentDirectory I need to append a filename to force it to look in node_modules of the currentDir.

@orta

orta commented Sep 14, 2021

Copy link
Copy Markdown
ContributorAuthor

Confirming to myself:

Revised code:

var localOverride = ts.resolveModuleName("@typescript/" + path, currentDirectory + "/tsconfig.json", { moduleResolution: ts.ModuleResolutionKind.NodeJs }, host);
console.log({ libFileName, localOverride, currentDirectory })

Does:

$ /Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/.bin/tsc
{
libFileName: 'lib.dom.d.ts',
localOverride: {
resolvedModule: {
resolvedFileName: '/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom/index.d.ts',
originalPath: undefined,
extension: '.d.ts',
isExternalLibraryImport: true,
packageId: [Object]
},
failedLookupLocations: [
'/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom.ts',
'/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom.tsx',
'/Users/ortatherox/dev/typescript/repros/tsLibOverrides/node_modules/@typescript/dom.d.ts'
]
},
currentDirectory: '/Users/ortatherox/dev/typescript/repros/tsLibOverrides'
}

The fact that whether the file exists is irrelevant (and so I think tsconfig.json is a bad call because it would feel like a mis-direction for someone reading) but it's not a very satisfying answer.

@orta

orta commented Sep 14, 2021

Copy link
Copy Markdown
ContributorAuthor

Looking through the code, this isn't necessarily on a hot route but this does affect isSourceFileDefaultLibrary which is called a bit in the watcher. I did successfully get a module cache not crashing via createModuleResolutionCache real late in the process but never resolved a successful result.

@orta

orta commented Sep 14, 2021

Copy link
Copy Markdown
ContributorAuthor

@typescript-bot pack this

@typescript-bot

typescript-bot commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

Heya @orta, I've started to run the tarball bundle task on this PR at e2ca4b5. You can monitor the build here.

@typescript-bot

typescript-bot commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

Hey @orta, 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/110482/artifacts?artifactName=tgz&fileId=D48E94F3C6C5280C83D295B2BD91EAE9FE2072B484565ED195437AEED0CAB59C02&fileName=/typescript-4.5.0-insiders.20210914.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/pr-build@4.5.0-pr-45771-10".;

@andrewbranchandrewbranch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One nit then excited to try this out

Comment threadsrc/compiler/program.ts Outdated
Comment threadsrc/compiler/program.ts Outdated
if (localOverride?.resolvedModule) {
return localOverride.resolvedModule.resolvedFileName;

const resolveFrom = combinePaths(currentDirectory, `/__lib_node_modules_lookup_${libFileName}__.ts`);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This always ignores currentDirectory because the latter argument is absolute.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Guessing this didn’t break tests only because all the tests are rooted at /

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

nice spot!

@orta

orta commented Sep 15, 2021

Copy link
Copy Markdown
ContributorAuthor

I explored setting the node_modules to not be root, but the currentDir is src in tests while unrooted files live at tests/cases/compiler/ in tests - so I either make it all arbitrary or I keep it just as arbitrary with a rooted node_modules

@mikob

Copy link
Copy Markdown

@orta

All JS dependency managers support this syntax:

has this been tested with yarn 3+? I get this error:

➤ YN0000: ┌ Resolution step
➤ YN0001: │ Error: @typescript/lib-dom@npm:@types/web isn't supported by any available resolver
at Xc.getResolverByDescriptor (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:294:5330)
at Xc.bindDescriptor (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:294:4719)
at p (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:303:6959)
at async Promise.all (index 1)
at async Fe.resolveEverything (/home/mikob/workspace/lipsurf/.yarn/releases/yarn-3.0.2.cjs:303:8166)

@mikob

Copy link
Copy Markdown

seems like a yarn issue, workaround here: yarnpkg/berry#1621

@orta

orta commented Oct 21, 2021

Copy link
Copy Markdown
ContributorAuthor

Did you use yarnpkg/berry#1621 (comment) e.g.:

"resolutions": {
"@typescript/lib-dom": "npm:@types/web@*"
}

With yarn add @typescript/lib-dom after? (We can add this to the types/web README)

@mikob

Copy link
Copy Markdown

I went the other route and just specified @* at the end of the dependency line.

@alshdavid

Copy link
Copy Markdown

Can this be imported explicitly without globals being set?
eg

import{window,Window,console}from'@typescript/lib-dom'

@dlemfh

Copy link
Copy Markdown

Great work! 👍

May I ask is it possible to use this feature to enforce my project to use specific node types e.g. @types/node@^14.17.0, while my dependency uses @types/node@^16?

What would I need to do if I wanted to not use node types altogether, even when my dependency is using node types?

Concrete use case would be: a dependency library (which uses node types) forcibly introduces type overrides for setTimeout, while my project (a web project) only ever needs the DOM version of setTimeout. I want to prevent @types/node from polluting my "type space".

@orta

orta commented Nov 3, 2021

Copy link
Copy Markdown
ContributorAuthor

Can this be imported explicitly without globals being set?

No, that's being researched over at #43972

May I ask is it possible to use this feature to enforce my project to use specific node types?

I don't think this can be too useful there - I think you might need some sort of pre-install script which checks. In theory #40468 could allow for it, but the underlaying issue is that everything in TS lives in a global type space and this at least lets you edit the 'ships with typescript' globals

@cdaringe

cdaringe commented Jan 25, 2023

Copy link
Copy Markdown

it's not clear to me exactly how to use this, having read the associated blog post.

  • i have a custom runtime (an emulator for a homegrown system with some globals)
  • it looks like we can only use existing lib names, so I'll attempt to squash "dom" as a proxy for my emulator types
  • using pnpm syntax, i'm linking in my lib DOM replacement via:
"dependencies": {// ...snip..."@typescript/lib-dom": "workspace:concordance-env@*"},
$ tree node_modules/\@typescript/lib-dom/src/
node_modules/@typescript/lib-dom/src/
└── index.d.ts
# all my good stuff!
// index.d.tsdeclarevarfoobar: number;

and specifying:

{"extends": "concordance-tsconfig/tsconfig.base.json","compilerOptions": {// DOM is squashed with concordance env :)"lib": ["DOM"]}}

but the compiler isn't picking up my symbols 🤔, such as foobar: number, as declared above.

did I miss a crucial step?

update--i had an import in my .d.ts which broke it :). removing the import made the symbols pick up.

@andrewbranch

Copy link
Copy Markdown
Member

Glad you figured it out, but it sounds like this feature isn’t what you want anyway. You’ve always been able to add arbitrary global type declaration files via the "types" key in tsconfig.json if they’re in a node_modules package, or "files"/"include" if they’re local, or /// <reference path="./path/to/globals.d.ts" /> to name a few. What this feature allows you to do is replace existing built-in lib types, like lib.dom.d.ts, while not breaking other dependencies that declare a dependency on those built-ins. If you just want to add global types to your compilation, just include your .d.ts file as you would any other, via one of the methods I mentioned above.

@cdaringe

Copy link
Copy Markdown

hey @andrewbranch, thanks!

You've always been able to add arbitrary global types

kind of! set me straight. these are the kind of global types inclusion by types config field and/or triple-slashes:

/// <reference path="..." /> - assumes types are local to my project (they're not!)
/// <reference types="..." /> - assumes they are @types/ prefixed node packages
/// <reference lib="..." /> - isn't a general "lib", is an enum of env specific libs

what's missing is:

/// <reference package="..." from my perspective.

due to reasons, i cannot publish to DefTyped. feasibly i could path="../../node_modules/foo/bar/index.d.ts, but that's cumbersome in my particular codebase.

for that reason, i thought "well, i could feasibly intercept one of the lib resolvers!". it ended up having other quirks, so i may pick a new path. but i'd love to have a <reference package-name='...' /> for this reason

@andrewbranch

andrewbranch commented Feb 2, 2023

Copy link
Copy Markdown
Member

Off the top of my head, I’m pretty sure both the "types" array and /// <reference types="..." />prefer looking up in typeRoots (i.e. node_modules/@types by default) first, but will fall back to looking for the plain package name in node_modules. Am I wrong about that? You can use tsc --traceResolution to audit the process used for every import and triple-slash directive to be sure.

@cdaringe

Copy link
Copy Markdown

Holy guacamole, why didnt i even try!? The docs are adequate, but I misinterpreted them. Thx

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Uncommitted BugPR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@orta@typescript-bot@mikob@alshdavid@dlemfh@cdaringe@andrewbranch@weswigham