型を上書きができる。型エラーが起こらないから使わない方が良い。
interface Test { foo: number } const test = {} as Test test.foo = 123 // OK!!
以下のようにいミューダブルなオブジェクトを作成する場合には使っても良い。
// resultの型が{}になってしまいエラーが起こる function shallowCopy<T extends object>(obj: T): T { const result = {}; for (const key in obj) { result[key] = obj[key]; } return result; } // T型をresultに当てることでエラーを防ぐことができる function shallowCopy<T extends object>(obj: T): T { const result = {} as T; // {} を {} as T に変更 for (const key in obj) { result[key] = obj[key]; } return result; }
参考