0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TypeScriptの「as」の使い方

Posted at

asの使い方

TypeScriptではasを型アサーションを行うために利用します。
つまり型情報を強制的に修正するときに利用されます。

(例)
type Info1 = {
  id: number;
}
type Info2 = {
  id: number;
  name: string;
}

function foo(obj: Info1): string {
  // ↓ これはエラーとなる
  // return obj.name;

  // objはInfo1型だが、無理やりInfo2型に修正して利用
  return (obj as Info2).name;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?