1
2

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.

【TypeScript】TypeScriptの基本的な型について

Posted at

はじめに

 本記事は、プログラミング初学者が、学習を進めていて疑問に思った点について調べた結果を備忘録も兼ねてまとめたものです。
 そのため、記事の内容に誤りが含まれている可能性があります。ご容赦ください。
 間違いを見つけた方は、お手数ですが、ご指摘いただけますと幸いです。

TypeScriptの基本的な型について

TypeScriptの型定義の方法

型を指定するためには、変数の後に:をつけ、その後ろに型を記述します。

TypeScriptの基本的な型一覧

TypeScriptの方について基本的なものは以下の通りです。

基本的な型一覧

// boolean
let bool: boolean = true;

// number 数値
let num: number = 10;

// string 文字列
let str: string = "ABC";
 
// Array 配列 (2通り)
// Arrayの後に配列の要素の型を記述する方法
let arr1: Array<number> = [0, 1, 2, 3];
// 配列の要素の型を記述し、その後ろに[]を記述する方法
let arr2: number[] = [0, 1, 2, 3];

// tuple (配列の要素の型を順番を含めて指定。)
let tuple: [number, string] = [4, "B"];

// any (どのような型を入れてもエラーが出ない。全ての型を許可。)
let any1: any = false

// void (関数が何も返していないことを示す。自動で予測されるため省略可能。)
const func1 = (): void => {
  const sample = "sample";
};

// null 
let null1: null = null;

// undefined
let undefined1: undefined = undefined;

// object
let obj1: object = {};
// オブジェクトの各プロパティの方を指定
let obj2: { id: number, name: string } = { id: 0, name: "田中" };

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?