11
8

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

TypeScriptの基本型まとめ

Last updated at Posted at 2014-11-14

Boolean(論理型)

var isDone: boolean = false;

Number(数値型)

TypeScriptのNumber型は実数も含みます。

var height: number = 6,
    width: number = 5.24;

String(文字列型)

var name: string = "Bob";
name = 'smith';

Array(配列)

1つ目の書き方

var list: number[] = [1, 2, 3];

2つ目の書き方

var list: Array<number> = [1, 2, 3];

Enum(列挙型)

C#の列挙型のような感じらしいです。

0番目の値が0のままで良い場合

enum Color {Red, Green, Blue};
var c: Color = Color.Green;

0番目の値を0以外にしたい場合①

enum Color {Red = 1, Green, Blue};
var c: Color = Color.Green; // Color.Green === 2

0番目の値を0以外にしたい場合②

enum Color {Red = 3, Green = 7, Blue = 9};
var c: Color = Color.Green; // Color.Green === 7

列挙名を取得する

enum Color {Red = 1, Green, Blue};
var colorName: string = Color[2];
console.log(colorName);

Any(Object型?)

どんな型でも入ります。

var notSure: any = 4;
notSure = "any型はどんな型でも入るンゴ";
notSure = false;

いろんな型のデータをリストっぽくしたいとき

var list:any[] = [1, true, 'strrrr'];
list[1] = 100;

Void

返り値なし。

function warnUser(): void {
    console.log('be careful.');
}

注意

Numberとnumberは区別されます。

原文(Handbook - Welcome to TypeScript)

11
8
1

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
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?