Last active
December 4, 2016 05:57
-
-
Save onionmk2/d45defdd61cce47123fdfb755c765e6e to your computer and use it in GitHub Desktop.
TypeScriptの関数の型
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
#1 関数宣言 | |
function funcName() : returnType => {} | |
*/ | |
function funcName (x) {} | |
function funcNameTs(x: number): number { return x;} | |
/* | |
#2 関数式 | |
*/ | |
/* | |
2.1 アロー関数での関数式 | |
() : returnType => {} | |
*/ | |
(x, y) => { return x + y;}; | |
(x: number, y: string): string => { return y + x;}; | |
/* | |
2.2 functionでの関数式 | |
function optinalFunctionName() : returnType {} | |
*/ | |
let hi1 = function funcExpression () { return "hi";}; | |
let hi2 = function funcExpressionTs () :string { return "hi";}; | |
let hi3 = function () { return "hi";}; | |
let hi4 = function () :string { return "hi";}; | |
/* | |
#3 関数型の変数の宣言 | |
let functionTypeVarTs: () => returnType; | |
*/ | |
let functionTypeVar; | |
let functionTypeVarTs: (x: number) => number; //これだけ (引数): 戻り値型 ではなくて (引数)=> 戻り値型 になる。 | |
/* | |
#4 関数式についての補足 | |
右辺か左辺のどちらかに型をつければ、もう一方はコンパイラが推測してくれる | |
*/ | |
// 両辺に型をつけても合法であるが... | |
let test1: (x: number) => number = (x: number) => { return x;}; | |
// 右辺式のみ型をつける | |
let test2 = (x: number) => { return x;}; | |
// 左辺式のみ型をつける | |
let test3: (x: number) => number = (x) => {return x}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
関数型の変数の宣言の、戻り値の型の指定方法が
let hoge () => returnType;
となる。=>
を使うのに注意する。