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

CSharperが書くTypeScript入門 Part1

Posted at

私自身がTypeScriptを触ったことが無いのでチュートリアルを順番に実施していきます。
※少し私のアレンジ加えます

今回は[Basic Types]の章を全て終わらせます。

参考

TypeScriptドキュメント

変数

TypeScriptの場合は「:」の後に型名を宣言することで型指定ができます。

Boolean

Boolean型はtrue,falseの設定が出来ます。

let isStarted: boolean = true;
let isDone: boolean = false;

Number

Number型は2進数、8進数、10進数、16進数が使用できます。
もちろん、浮動小数点数も使用できます。

let decimal: number = 6; // 10進数
let hex: number = 0xf00d; // 16進数
let binary: number = 0b1010; // 2進数
let octal: number = 0o744; // 8進数
let pi: number = 3.14; // 小数

String

文字列はシングルクォート(')またはダブルクォートで囲います。

let color: string = "blue";
color = 'red';
let name: string = 'iwakura';
let age: number = 24;
// ES6のテンプレート文字列も使用できる
let profile: string = 'Name:${name}, Age:{$age}';

Array

配列は[]を使用します。

let list: number[] = [1, 2, 3];
let list2: string[] = ['a', 'b', 'c'];
// ジェネリックも使用できるらしい
let list3: Array<number> = [1, 2, 3];

Tuple

タプルって言っても、配列を複数の型で宣言するだけみたいですね。
以下の様に宣言したした場合は
[0]:string
[1]:number
[2]:string
[3]:number
と、交互に割り当てられるみたいです。

let x: [string, number];
x = ["test1", 10];
console.log(x[0]);
console.log(x[1]);
// 出力結果
// test
// 10

Enum

列挙型も使用できるのですね。めっちゃC#みたい。
そして中括弧だからセミコロンはいらないのね。
ちなみに数字を明記しない場合は1から始まります。

enum Color {Red, Green, Blue}
let c: Color = Color.Green;
// もちろん、数字を割り当てることもできる
enum Num {Zero = 0, One = 1, Two = 2}
console.log(c);
// 出力結果
// 1

Any

Anyを使用すると型が不明な状態で宣言できます。

let obj: any = 4; // 数値を代入
// 後から別の型を代入しても怒られない
obj = 'Object';
obj = true;
// 配列で使用するとごちゃ混ぜにできる
let list: any[] = [1, true, "文字列"];

Void

公式ドキュメントに記載されている通りだといきなりVoidがきました。
まだ関数宣言すらやってないのに・・・
C#とかJavaと同じで、「戻り値なし」の関数宣言用ですね。

function hoge(): void {
    alert("hoge hoge");
}

Null and Undefined

nullとundefinedの扱いはJSと同じで別らしいです。

let u: undefined = undefined;
let n: null = null;

Never

ん?何これ・・・

The never type represents the type of values that never occur.
訳) never型は決して出現しない値の型を表す。

どうやら、関数の中でthrowで返す時や、そもそも結果を返さない時に使用するらしいです。

// throwで返す
function error(message: string): never {
    throw new Error(message);
}

// 呼び先でtrhowを返すため、暗黙的にneverと解釈する
function fail() {
    return error("Something failed");
}

// 結果を返さない
function infiniteLoop(): never {
    while (true) {
    }
}

Type assertions

キャストのことみたいですね。

/*
* ブラケット(角括弧)によるキャスト
*/
// 最初はanyで定義
let someValue: any = "this is a string";

// 使う時にstringへ変換
let strLength: number = (<string>someValue).length;

/*
* asによるキャスト
*/
let someValue: any = "this is a string";

let strLength: number = (someValue as string).length;

どちらでキャストするかは自由らしいですが、JSXでTypeScriptを使用する場合は「as」によるキャストしか使えないらしいです。

最後に

公式には「let」について軽く書かれていますが、ここでは説明しません。
letを使用することでスコープを意識した実装が行えるので、極力「let」を使いましょう。

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