microsoft / microsoft/TypeScript

A flag to make TypeScript more strict towards duck-typing classes

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

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

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

説明

Suggestion

🔍 Search Terms

JavaScript class, TypeScript interface, duck-typing, anonymous object

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I think it might be useful if TypeScript didn't always treat classes same as interfaces. For example, I'd like to see at least a warning when assigning a compatible anonymous object to a variable of a known class type, like this (Playground link):

class C { s: string = ""; }
const o: C = { s: "123" };
console.log(o.s);
console.log(o instanceof C); // false
console.log(o.constructor.name); // Object

📃 Motivating Example

The above code compiles without any warnings, which may come as a surprise for people coming from other OOP languages, and potentially cause runtime errors.

Unlike TypeScript interfaces, classes are still "first-class citizens" in JavaScript. I believe they're are essential part of JavaScript, and their value as types should rather be elevated by TypeScript, than diminished. This might help people coming to TypeScript from other languages (like C#, Java and even JavaScript itself!) who extensively have used classes before.

In this light, the author of that code fragment probably meant to write something that looks like below (Playground link). It'd be great if TypeScript introduced a CLI option to assist with spotting cases like that.

class C { s: string = ""; }
const o: C = Object.assign(new C(), { s: "123" });
console.log(o.s);
console.log(o instanceof C); // true
console.log(o.constructor.name); // C

On a side note, if I do this (Playground link):

class C { s: string = ""; }
const o: C = new C();
const o2: C = new o.constructor();
console.log(o2.constructor.name);

I don't understand the warning "This expression is not constructable. Type 'Function' has no construct signatures.(2351)" for the const o2: C = new o.constructor() line. It looks perfectly valid to me, the class type should be deductible in the above code fragment.

I can think of const o2: C = new (o.constructor as {new(): C})() as a workaround, or const o2: C = new (o.constructor as any)() as C. I don't like either, because at least the base class type for o is already known in this lexical scope, it's C.

💻 Use Cases

I hope I've covered that with the second code fragment, but also please see @Barbiero's comment below. Thanks for considering this idea.

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

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

はじめの一歩

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

調査の方向性

この issue ではリポジトリのファイルやテストが指定されていないため、まず 2 つの TypeScript Playground の例から始め、現在の診断とランタイムコメントを比較します。匿名オブジェクトをクラス型に割り当てる場合と o.constructor に対する意図した opt-in の動作を定義し、要求された診断と既存の動作が変更されていないことをカバーするテストによって完了を示します。

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

評価

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

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

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