- 「関数からの戻り値がないことを期待する型」をvoid型として扱う。
C言語のvoidとは意味が異なる
TypeScriptにおけるvoid型
- Void => 何もデータが存在しない時に指定
もし、
(1) return文で明示的にundefinedを指定するのは、本筋から逸れる
function nothing(): undefined { console.log ("hoge"); } console.log(nothing);
コンソールに'undefined'と表示。
この関数は何もreturnしない。 これはconsole.logがundefinedと出力。
return文で明示的にundefinedを指定するのは、本筋から逸れる
function nothing(): undefined { console.log ("hoge"); }
=> Error発生。 => A function whose declared type is neither 'void' nor 'any' must return a value. => (void型やany型では、型宣言[型注釈]が必要ない。)
return文で明示的にundefinedを指定するのは、本筋から逸れる
function nothing(): undefined { console.log ("hoge"); return undefined }
annotation (型アノテーション [型注釈])
前述のように、:TypeAnnotation構文を使って型アノテーションを書きます。型宣言空間で使用可能なものは、型アノテーションとして使用できます。 次の例は、変数、関数パラメータ、および関数戻り値の型アノテーションを示しています。
let num: number = 123; function identity(num: number): number { return num; }