microsoft / microsoft/TypeScript

Suggestion: Type-check statement to verify type assignability

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

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

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

説明

Search Terms

type check statement compile (compilation) time

Suggestion

I wanted to check compatibilities for types from different (especially external) modules in compilation time.

import { SomeType } from 'some-external';
// ... something statement here to check if SomeType is not changed
// by update for 'some-external' or etc.

To solve it, I suggest to add type-check statement like following:

// Compile error if 'SomeType' does not satisfy the constraint 'WantedType'
//   (similar to error TS2344)
type assert SomeType extends WantedType;
// The message "Unexpected 'SomeType'" will be output if the error occurs
// - The trailing expression (message clause) must be a valid string literal
// (the message clause is not necessary for this suggestion
//  but would make easier to solve the error...)
type assert SomeType extends WantedType, "Unexpected 'SomeType'";
  • The type-check process should be same as the process for type constraints in type parameters.
  • There is already type statement, but the above statements are not conflicted because type statements requires = after BindingIdentifier (or TypeParameters).
  • Similar to #10421, but this type assert only check types; it does not cast variables.
  • extends keyword may confuse to one used in conditional type. (not ambiguous?)
    • For type assert A extends B, both A and B should accept conditional type.
  • (type assert is somewhat inspired by other languages' assert statements (especially static_assert in C/C++), but I don't know whether the words type assert are the best...)

Use Cases

  • Check compatibilities for types simply and easily
    • Useful to avoid using incompatible package versions with another packages/modules.
  • Assert types explicitly with more human-readable
    • This would not be useful for compilers/implementers, but would be useful for developers to read codes.
  • Test type definition (e.g. for unit test)

Currently we can check types statically as following:

import { SomeType } from 'some-external';
// Helper definition to check type
type TypeCheckerOfWantedType<T extends WantedType> = T;
// Error if 'SomeType' does not satisfy 'WantedType'
type CheckResultForSomeType = TypeCheckerOfWantedType<SomeType>;
// this is necessary to avoid "'CheckResultForSomeType' is declared but never used"
declare global { var _dummyVariableForCheckSomeType: CheckResultForSomeType; }

While no JavaScript codes are generated from this code, this is more complex to check.

Examples

// Must not be an error
type assert Element extends Node;
// Error in 'es5', pass in 'es2015'
type assert ObjectConstructor extends { assign: (...args: any[]) => any; },
  'Object.assign() is not available';

import { User } from 'db-library';
// Error if User does not have 'id' member with type assignable to 'number'
type assert User extends { id: number; }, 'Unexpected User type';

const a = someFunction();
if (typeof a === 'string') {
    // treat 'a' as 'string'
} else {
    // Error if narrowed type of 'a' does not have '{ data: string }'
    type assert typeof a extends { data: string; };
    // (this assert does not mean that type of 'a' is treated as '{ data: string }' here)
}

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 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提案されている type assert 構文、その型制約の動作、条件型のケース、および既存の type 文との相互作用を確認します。提案を現在の型チェックおよびパースのアーキテクチャと比較し、その後、受け入れられるアサーション、代入可能性エラー、オプションのメッセージ、型が絞り込まれた式を対象とするテストを定義します。構文が仕様化され、その動作がテストでカバーされれば完了です。

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

評価

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

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

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