microsoft / microsoft/TypeScript

Object spread works for unconstrained generic

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

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

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

説明

TypeScript Version: 3.8.3

Search Terms:
generic
spread
unrestrained
unrestricted

Expected behavior:
The spread function should not compile because T is not constrained to a type that can be spread.

Actual behavior:
It compiles.

Related Issues:
https://github.com/microsoft/TypeScript/issues/30129
https://github.com/microsoft/TypeScript/pull/13288

Code

function spread<T>(t: T) {
  // This should not compile
  return { ...t };
}

// The type of `result` is `number` even though the value of `result` is `{}`
const result = spread(5);

Output
"use strict";

function spread(t) {

    // This should not compile

    return Object.assign({}, t);

}

// Result is still a number when in reality it's {}

const result = spread(5);


Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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

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

はじめの一歩

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

調査の方向性

提供された Playground リンクの動作を、TypeScript 3.8.3 と記載されたコンパイラーオプションを使って再現します。関連する issue と pull request を読み、unconstrained generic に対する object spread をコンパイラーがどのように処理しているかを追跡します。サンプルが期待どおりに拒否され、結果の型が誤って引数の型のままにならなくなれば完了です。

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

評価

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

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

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