結論
見てもらったほうが速いので
実行環境をを示します。
Typescriptを使って型mappingしたら 思っていた意図と違う結果が返ってきました。
型 Shipから 型 AnotherShip に内容をマッピングするという
単純なコードです
コード
Sorahune.ts
// 船
interface Ship {
id : number
name: string
cap: string
member: string
}
interface AnotherShip {
id : number
name: string
member:string
}
const sorafune: Ship = {
id: 1,
name: "sonofune",
cap: "omae",
member:"TOKIO",
}
const map: AnotherShip = { ...sorafune }
//期待としては capというプロパティははなくなっていてほしい
console.log(map)
結果
javascriptにコンパイルされた結果は以下のようになってます。
Object.assainメソッド[mozilla]
を見ていただければわかるように、Objectを丸ごとコピーしており、
capプロパティは削除されていません。
Sorahune.js
"use strict";
const sorafune = {
id: 1,
name: "sonofune",
cap: "omae",
member: "TOKIO",
};
const map = Object.assign({}, sorafune);
console.log(map);
実際のコンソール画面です。
当たり前ですが、仮にmapping先(AnotherShip)に
Shipにないプロパティが存在していた場合、そこでコンパイルエラーになります。
ではでは。