microsoft / microsoft/TypeScript

"types" field in package.json pointing to a `.ts` file in node_modules results in the file being compiled and type checked

オープン
#35,744 コメント 7 件 リアクション 5 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Continuing from https://github.com/Microsoft/TypeScript/issues/22228

cc @evil-shrike

I have this issue.

@mhegazy said

the lib has either "types": "index.ts" or a .ts file at its root. which is wrong. a library should not expose its sources, only its declarations.

But features like inferred return types (f.e. when making class-factory mixins) are not compilable to declaration files, resulting in errors like

error TS4025: Exported variable 'html' has or is using private name 'htmlBind'.
error TS4031: Public property '_currentArea' of exported class has or is using private name 'AreaInternal'.
error TS4055: Return type of public method from exported class has or is using private name 'PartHelper'.
error TS4073: Parameter 'partHelper' of public method from exported class has or is using private name 'PartHelper'.
error TS4078: Parameter 'options' of exported function has or is using private name 'ExtendOptions'.

A .d.ts does not have expressions.. it represents the shape of the API.

Not entirely true.

As far as I know, the only way to use features (that declaration files don't support) in downstream projects is to get types directly from .ts source files. This makes the need to point types to .ts source files a valid use case.


This is what I think should happen:

If "types" points to a .ts file, and "main" points to a .js file, then the compiler should use the .ts file only for type definitions and not compile or type-check the code.

"main" can serve as a guide to telling the compiler whether it should compile sources, or read js files. "types" should be for... specifying the source of types.

Unless I missed it, there's no other way to include types for features that aren't representable in declaration files.

Why is it that declaration features don't match source features? It seems that an important goal should be for declaration features to always have the capability of matching source features.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、ここで説明されている package.json の設定を再現します。types は .ts ファイルを、main は .js ファイルを指すようにします。背景については issue #22228 の議論を確認してください。完了条件は、コンパイラが型情報に .ts のエントリを使用し、その実装をコンパイルも型チェックもせず、要求された downstream の型付け動作を維持することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。