[TypeScript] 2.5¶
TypeScript2.5のリリース内容まとめ
Optional catch clause variables¶
HAD BETTER EASY
try-catchのcatch節でエラーオブジェクトの宣言を省略できるようになった。
let result: Object;
try {
result = JSON.parse("{hoge hoge}");
console.log("ok");
} catch (_unuseError) {
// 使わなくても _unuseError を宣言しないといけない
console.log("parseにしっぱい");
}
let result: Object;
try {
result = JSON.parse("{hoge hoge}");
console.log("ok");
} catch {
// 使わない場合は _unuseError の宣言を省略できる
console.log("parseにしっぱい");
}
Type assertion/cast syntax in checkJs/@ts-check mode¶
NOT NECESSARY EASY
JavaScriptの式に対して、キャストおよび型アサーションができるようになった。
式
に対して、/** @type {キャストしたい型} */ (式)
と書く。
関数の中で式のキャストが必要なときに使える。
/**
* @param {any} [a]
* @param {any} [b]
*/
export function hoge(a, b) {
// a + b の結果をstringにキャスト
const x = /** @type {string} */ (a + b);
// 以降でxはstring型と判定される
// xはstringになっている
return x.startsWith("1");
}
クラスなどでもOK。
import { initHuman } from "./plain";
initHuman({id: 100, name: "A hundred"});
// =>
// 100
// A hundred
class Human {
/**
*
* @param {number} id
* @param {string} name
*/
constructor(id, name) {
this.id = id;
this.name = name;
}
}
/**
* @param {any} humanLike
*/
export function initHuman(humanLike) {
const humanInstance = /** @type {Human} */ (humanLike);
console.log(humanInstance.id);
console.log(humanInstance.name);
}
Deduplicated and redirected packages¶
NOT NECESSARY EASY
name
、version
共に同一のpackageが既に登場していたら、リダイレクトするようになった。
これは、moduleResolutionがNode
の場合のみ。
重複がなくなるため、パフォーマンスや宣言の不整合リスクが下がったりする。
The --preserveSymlinks compiler flag¶
NOT NECESSARY EASY
--preserveSymlinks
フラグが追加された。
このフラグを有効にすると、依存モジュールにシンボリックリンクが存在するとき挙動が変わる。
--preserveSymlinks | 識別子 | モジュールのrootパス |
---|---|---|
有効 | リンクファイルの名称 | リンクファイルの場所 |
無効 | リンクが指す実際の名称 | リンクが指す実際の場所 |
peer dependenciesを解決するとき、必要になることがある。