Type unification not working for nested types

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
typescript
領域
compilers

調査の方向性

提供された Playground Link から始め、記載されたコンパイラオプションで課題を再現し、TypeScript 3.3.3 と 3.5.1 以降のバージョンを比較します。ネストされたジェネリック型がどのように統合されるかを追跡し、無関係な代入可能性チェックを弱めることなく、示されたプログラムがコンパイルできたら完了を確認します。

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

説明

Bug Domain: check: Variance Relationships

TypeScript Version: 3.7.2, 3.6.3, 3.5.1 (but not 3.3.3)

Search Terms:
type unification

Explanation for the code:
We have a collection of different document types (think Firebase's Cloud Firestore if you're familiar with that). Each document type of which has a type associated with it which contains a few fields like the document type, the backend return type, etc. Then we have a number of functions in our store that take in one of these types and use the specific fields that they need. (Some functions will look at the foo field, some will not.) It's not at all uncommon for these to be passed through lots of wrapping functions until the last level pulls out the field that it needs.

It is nice sometimes to be able to share code between two different (but similar) collection types. We can do if say their document type is the same (when two different endpoints return the same document). With recent versions of TypeScript we can only do that sharing if we expand the types at the place of definition.

Code

// A type that just pulls the foo field out of the type
type JustFoo<T extends { foo: any }> = T['foo'];

// This type is unused, but we have types like this too.
type JustOtherField<T extends { otherField: any }> = T['otherField'];

// We then wrap this in another type
type WrappedJustFoo<T extends { foo: any }> = { wrapped: JustFoo<T> };

// We then have a lot of types that have a `foo` field and `otherField` fields.
// These types will be passed into a lot of other functions, some of which
// will only use the `foo` field, where as other function may use other parts.

type Alpha = { foo: number; otherField: string };
type Beta = { foo: number; otherField: boolean };


// We can define variables then using `WrappedJustFoo`.
const alpha: WrappedJustFoo<Alpha> = { wrapped: 3 };
const beta: WrappedJustFoo<Beta> = { wrapped: 3 };

// Sadly we can't share them even though ` WrappedJustFoo<Alpha>` and
// `WrappedJustFoo<Beta>` actually resolve to the same type.
const beta2: WrappedJustFoo<Beta> = alpha;

// If we use the resolved type we can convert either way.
let gamma: { wrapped: number } = alpha;
let beta3: WrappedJustFoo<Beta> = gamma;

Expected behavior:
The program compiles.

Actual behavior:

Type 'WrappedJustFoo<Alpha>' is not assignable to type 'WrappedJustFoo<Beta>'.
  Type 'Alpha' is not assignable to type 'Beta'.
    Types of property 'otherField' are incompatible.
      Type 'string' is not assignable to type 'boolean'.(2322)

It makes sense that if you want to check that 'WrappedJustFoo<Alpha>' is assignable to type 'WrappedJustFoo<Beta>' you only check if 'Alpha' is assignable to type 'Beta', but this isn't the only way they can be compatible.

Related Issues: Nothing jumps out at me. #30134 maybe?

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

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

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

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

はじめの一歩

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

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

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

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