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 3 years have passed since last update.

【実務未経験】初学者が0から学ぶTypeScript③

Last updated at Posted at 2020-10-11

参考:TypeScriptの型入門, TypeScript Deep Dive 日本語版
前回:【実務未経験】初学者が0から学ぶTypeScript②
※もし私の記事に誤りがありましたら、ご教示いただけますと幸いです。

##配列型
配列が持つデータに型を付けられる。

const 定数名: 配列の型名[]

const a: number[] = [1,2,3,4];
const b: boolean[];

a.push(5);
b = [true, false, true];

a.push("maguro");
//aの配列には数値しか入らないので型エラー

##関数型
関数の引数や返り値に型を付けられる。

const 関数名:(引数名:引数の型名) => 返り値の型名
function 関数名(引数名:引数の型名): 返り値の型名 {行う処理};

const checkTuna:(tuna: string)  => boolean;

返り値がない時はvoidという型を指定できる。

void :中身がない、空っぽの、役に立たない、効果がない

function showTuna(): void {
  console.log('まぐろ');
};

##nullとundefined
null型とundefined型は他の型に代入できる。
TypeScript Deep Dive 日本語版-nullとundefined

const fishName = string;
const fishSize = number;
fishName = undefined;
fishSize = null;
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?