microsoft / microsoft/TypeScript

A way to express that a method must be overriden

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

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

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

説明

Suggestion

In Javascript it is possible and encouraged in documentations like MDN to override toString and valueOf for custom objects, because the version inherited from Object is not very useful (the dreaded [object Object]).

However, there is to my knowledge no way to express in a type that a class must override those methods (or that an object must have them as own properties) and therefore no way to know if they are reliable.

🔍 Search Terms

method overriding, toString, own property

✅ 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

An idea would be to have a own modifier which would basically compare a property name with the list produced by the following function

const ownModifierList = (obj) => {
  const isObjectLiteral = obj.constructor.name === "Object";
  const toList = isObjectLiteral ? obj : obj.constructor.prototype;
  return Object.getOwnPropertyNames(toList).filter(x => x !== 'constructor')
}

I think it reads well:

type showable = {
   own toString: () => string
}

📃 Motivating Example

Having a useless toString is borderline a Liskov substitution violation and Javascript users have learned to not rely on it. Typescript could come to the rescue.

The modifier would also make it easy to reject Object.create(null).

I have been reading some Haskell lately and I was envious of the confidence the type class Show gives haskellers, but it is built in. Javascript uses toString for type coercion and I believe it makes more sense to try to achieve that level of confidence by fixing it.

I don't know in what other circumstances one would require that a method is overridden as it would indicate that the base class's method is not suitable, which would be a red flag, but my lack of imagination is no proof that it can't be useful in other situations.

💻 Use Cases

Having a polymorphic way to query a string from an object.

Currently one would need to implement a custom method or signify by some tag that toString is safe to use, and the user of the object would need to know about this tag/custom method.

We can imagine generic functions like a sorting function which default to converting an object to a string in order to deal with it, but only if it is a usable string. The IDE would advertise by the function definition in the tooltip that it is possible to pass an object as long as it implements toString, which would make the user happy as it would potentially clean up some code in many places.

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

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

はじめの一歩

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

調査の方向性

提案されている own 修飾子と、toString および valueOf に関する例から始めます。この issue では、実装ファイル、テスト、エントリーポイントは指定されていません。own プロパティや Object.create(null) を含め、意図された型システムの動作を特定し、要件が受け入れられる場合と拒否される場合を示すテストを定義します。

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

評価

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

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

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