microsoft / microsoft/TypeScript
Represent the types of function parameters that mutate inside the function.
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
In JavaScript is possible to mutate objects inside functions. Right now, the following code in JavaScript:
function merge(x,y) {
Object.assign(x,y);
}
let x = {a: 1};
merge(x, {b: 2});
console.log(x.b);
Can't be written in TypeScript without casting the type. There are a few options whose type definition is wrong in all scenarios I can think of (maybe I'm missing a better option):
Option 1
let x: {a: number, b: number} = {a: 1}; // Error, missing b
merge(x, {b: 2});
Option 2
let x: {a: number, b: number} = {a: 1, b: 2};
merge(x, {b: null});
// From here, x.b is not a number anymore, but you could do
let y: number= x.b;
Suggestion
There could be an extension to function parameter definition like the following:
// then keyword indicates that before it can be type A, and after it will be of type A&B.
function merge<A,B>(x: A then x2: A&B, y: B) {
Object.assign(x2,y);
}
let x: {a: number, b: number} = {a: 1, b: 2};
merge(x, {b: null});
// Here, type of x is {a: number, b: number} & {b: null}
x.b; // Type null
There, we indicate that whatever type was x before, now it is something different. The code above could be written in TypeScript as follows:
// then keyword indicates that before it can be type A, and after it will be of type A&B.
function merge<A,B>(x: A, y: B) {
Object.assign(x,y);
}
let x: {a: number, b: number} = {a: 1, b: 2};
merge(x, {b: null});
// Here, type of x is {a: number, b: number} & {b: null}
let xAfterMerge = x as {a: number, b: number} & {b: null};
// Since this line, x should not be used but xAfterMerge
xAfterMerge.b; // Type null
Another example
interface Before {
address: string;
}
interface After {
addr: string;
}
function map(userb: Before then usera: After) {
usera.addr = userb.address;
delete userb.address;
}
let u = {adress: "my street"};
map(u);
console.log(u.addr);
That could be syntax sugar for this:
interface Before {
address: string;
}
interface After {
addr: string;
}
function map(userb: any) {
userb.addr = userb.address;
delete userb.address;
}
let u = {adress: "my street"};
map(u);
console.log((u as After).addr);
Syntax
It could be something like:
identifier: type *then* identifier: type
With the identifiers being different, and with the types being mandatory an extension of Object.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提案されている merge と map の例を確認し、提案されている then パラメーター構文と、変更前後の型の挙動を含めて検討します。提案に、ミューテーション、エイリアス、プロパティの削除、nullability について確定したセマンティクスがあるかを判断します。完了とするには、実装とテストの指針にできるほど正確な、合意済みの設計が必要です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100