Allowing relative paths in triple slash "types" directives generates broken definitions
@weswigham がすでに取り組んでいます。
2020年2月26日 から。
評価
この issue はまだ評価されていません。
説明
I managed to publish a module with unusable type definitions due to incorrect use of triple slash directives in one of my dependencies. I think Typescript could and should have caught this as an error when generating declarations during compilation.
TypeScript Version: 3.8.0-dev.20200131
Search Terms:
triple slash directive relative path
Code
Let's build a minimal app that consumes a custom type definition. For convenience I'll place that in types and specify typeRoots but this also applies to published modules which might appear in node-modules/@types/some_module.
├── package.json
├── tsconfig.json
├── src
| └── main.ts
└── types
└── package
├── index.d.ts
└── type.d.ts
package.json
{
"name": "reference-type-paths",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "rm -rf dist/*; tsc"
},
"devDependencies": {
"typescript": "^3.8.0-dev.20200131"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"typeRoots": ["./types"]
}
}
We setup some TS file to use a custom type defined in a module.
src/main.ts
export class Container {
readonly data: Optional<string>;
}
Unfortunately our module has incorrectly used a types triple slash directive with a path.
types/package/index.d.ts
/// <reference types="./type" />
types/package/type.d.ts
declare type Optional<T> = T | null | undefined;
Finally we run tsc.
Expected behavior:
Typescript should not emit declaration files which contain invalid "types" paths. Use of /// <reference types= with a path (or a path which would not resolve relative to typeRoots?) should be an error during compilation to prevent generating unusable declaration files.
We have /// <reference path= which should have been used here instead. If types/package/index.d.ts contains /// <reference path="./type.d.ts" /> Typescript produces:
dist/main.d.ts
/// <reference types="package" />
export declare class Container {
readonly data: Optional<string>;
}
Which would then resolve correctly.
Alternately a possibly valid but fragile output might be to generate a path relative to the typeRoots directory:
dist/main.d.ts
/// <reference types="package/type" />
export declare class Container {
readonly data: Optional<string>;
}
https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types- states:
The process of resolving these package names is similar to the process of resolving module names in an import statement. An easy way to think of triple-slash-reference-types directives are as an import for declaration packages.
which doesn't suggest that relative paths are inappropriate or dangerous in triple slash type references.
Actual behavior:
Unfortunately what actually get is the preserved relative path from the module's triple slash directive.
dist/main.d.ts
/// <reference types="./type" />
export declare class Container {
readonly data: Optional<string>;
}
This definition is unusable. If we publish it consumers of our module will see errors because type does not appear relative to their typeRoots.
This is especially frustrating because may only be noticed in second level dependents of the module with the incorrect directive. In this example I can depend on package, apparently successfully compile and generate my own declarations, publish them, and then someone depending on my package hits compile errors attempting to consume my declarations.
Playground Link:
Not possible, depends on generating d.ts declaration files.
Related Issues:
https://github.com/microsoft/TypeScript/issues/35343
https://github.com/microsoft/TypeScript/issues/15559
https://github.com/microsoft/TypeScript/issues/30523
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/TypeScript のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
microsoft/TypeScript#64322 · コメント 2 件 · リアクション 1 件 · 担当者 2 名 ·
-
Possible Improvement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
microsoft/TypeScript#64278 · コメント 1 件 · リアクション 1 件 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
microsoft/TypeScript#64118 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
microsoft/TypeScript#64094 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
microsoft/TypeScript#63959 · コメント 5 件 ·
microsoft/TypeScript の issue をすべて見る
似ている issue
-
optimization optimization:agents-md-curator
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
githubnext/gh-aw-cao#13143 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
blinklabs-io/bursa#904 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · コメント 2 件 ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100