microsoft / microsoft/TypeScript

Add typed overloads to `Object.create`

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

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

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

説明

Search Terms

  • object.create
  • object create
  • typed object.create
  • typed object create

Suggestion

Change the signatures of Object.create to:

interface ObjectConstructor {
	create<P extends object | null>(proto: P): null extends P ? {} : P;

	create<
		P extends object | null,
		M extends PropertyDescriptorMap = {}
	>(proto: P, properties?: M & ThisType<any>): {
		readonly [
			K in keyof M as M[K] extends { readonly writable: false }
				? K
				: never
		]: M[K] extends Readonly<TypedPropertyDescriptor<infer V>>
			? V
			: any;
	} & {
		-readonly [
			K in keyof M as M[K] extends { readonly writable: true }
				? K
				: never
		]: M[K] extends Readonly<TypedPropertyDescriptor<infer V>>
			? V
			: any;
	} & (null extends P ? {} : P);
}

Use Cases

Getting correct types in code that does:

// `foo` is `any`, because `T` of Object.assign<T, U> is `any`:
const foo = Object.assign(
	// `T` is currently typed as `any`:
	Object.create(null),

	// Only `U` is typed correctly as { bar: number; baz: string }:
	{
		bar: 123,
		baz: 'biz',
	},
);

Examples

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • This might need a flag similar to strictBindCallApply, or just done in a separate lib file.
  • 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.

Relevant issues

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

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

はじめの一歩

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

調査の方向性

提案されている ObjectConstructor.create のオーバーロードと、リンクされている issue #1108、#13206、#18609、#38385、#41424 から始めてください。現在のライブラリ宣言と互換性に関する懸念をどのように扱うべきかを判断してください。Object.create が JavaScript のランタイム動作を変更せずに要求された型情報を提供できれば完了です。

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

評価

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

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

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