microsoft / microsoft/TypeScript

allow local module type declarations on separate files (similar to C .h files)

オープン
#38,914 コメント 0 件 リアクション 3 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

Search Terms

.d.ts definitions ambient module typing local

Suggestion

I would like to being able to compose typescript modules/components inside my application .i.e. src/components/Foo.ts and being able to declare its typings under src/components/Foo.d.ts where typescript would either infer those types automatically.

Disclaimer: It may happen that this is already possible, but I already researched everywhere and read the documentation as well as trying to declare ambient modules (which won't work with relative paths), so I am trying this as a last resort in case it's indeed not currently possible.

Use Cases

Sometimes, when creating components inside an application, some may have a quite extensive type set which usually makes the file difficult to read and take the focus away from the implementation.
Like so:

src/component/Foo.ts
##########################
#         type           #
#         type           #
#         type           #
#         type           #
#         type           #
#                        #
#    implementation      #
#    implementation      #
##########################

This would make it easier for the developer to organise their code similar to C's .h files separating the definition from implementation.

Examples

Project structure:

src/components
src/components/Foo.ts
src/components/Foo.d.ts
src/components/complex/index.ts
src/components/complets/index.d.ts

types are automatically inferred within the main module

src/components/Foo.ts
const bar:Bar = {foo:1,bar:2};
export default ...
src/components/Foo.d.ts
type Bar = {foo:number; bar:number; }
export type ExportedType = string[];

types can still be imported from a different component

src/components/Bar.ts
import type {ExportedType} from './Foo';
import type {Bar} from './Foo'; // < Error as Bar is not exported

This would result in:

src/component/Foo.d.ts                    src/components/Foo.ts
##########################                ##########################
#         type           #                #    implementation      #
#         type           #                #    implementation      #
#         type           #                #                        #
#         type           #                #                        #                        
#         type           #                #                        #
##########################                ##########################

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

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

はじめの一歩

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

調査の方向性

提案されている src/components/Foo.ts と src/components/Foo.d.ts の例を、src/components/Bar.ts に示されている import を含めて、再現の出発点として使用してください。まず、TypeScript が現在相対モジュールの宣言をどのように解決しているかを調査し、次に Bar と ExportedType の想定される可視性を確認してください。Foo.ts に個別の宣言ファイルが関連付けられ、指定された export の動作が維持されれば完了です。

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

評価

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

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

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