TypeScript の公式サイトの Playground で次のように TypeScript のコードを打ち込みました。
https://www.typescriptlang.org/play
typescript
1const message: string = 'hello world'; 2console.log(message); 3 4function hello(message: string) { 5 console.log(message); 6} 7 8hello(100);
js
1"use strict"; 2const message = 'hello world'; 3console.log(message); 4function hello(message) { 5 console.log(message); 6} 7hello(100);
hello 関数は、定義時に引数に文字列を指定していて、実行時に数字を渡しているのにも関わらず、エラーが発生せずに実行できていますが、なぜでしょうか?
次のようなエラーが出るものと想定していました。
error TS2345: Argument of type '100' is not assignable to parameter of type 'string'.
ご助言いただければありがたいです。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/09 00:37