前提・実現したいこと
TypeScriptを勉強し始めたのですが、実際にコードを書いてみまして、strictをtrueにしていてもundefinedが紛れ込むケースがありました。
なぜなのか考えてみたのですが、わからず教えていただければうれしいです。
よろしくお願いいたします。
該当のソースコード
typescript
1function goToDisney( humanCount: number, humanNames: string[] ): string{ 2 console.log(humanCount); 3 console.log(humanNames); 4 return humanNames[0]; 5} 6 7type ParamGoToDisney = Parameters<typeof goToDisney>; 8 9type ReturnGoToDisney = ReturnType<typeof goToDisney>; 10 11const humanNamesValue: ParamGoToDisney[1] = []; 12 13const doneGoToDisney: ReturnGoToDisney = goToDisney( 5, humanNamesValue ); 14 15console.log(doneGoToDisney); 16
log(結果)
行ったことなど
string[]
からNoEmptyArrayType<T> = [T, ...T[]]
に変更することで一応は大丈夫かな?と思ったのですが、本当にこれでいいのかな..?と思っております。
typescript
1type NoEmptyArrayType<T> = [T, ...T[]]; 2 3function goToDisney( humanCount: number, humanNames: NoEmptyArrayType<string> ): string{ 4 console.log(humanCount); 5 console.log(humanNames); 6 return humanNames[0]; 7} 8 9type ParamGoToDisney = Parameters<typeof goToDisney>; 10 11type ReturnGoToDisney = ReturnType<typeof goToDisney>; 12 13const humanNamesValue: ParamGoToDisney[1] = []; // Property '0' is missing in type '[]' but required in type '[string, ...string[]]'. 14 15const doneGoToDisney: ReturnGoToDisney = goToDisney( 5, humanNamesValue ); 16 17console.log(doneGoToDisney); 18 19
補足情報(FW/ツールのバージョンなど)
TypeScript : v3.9.2
Strict Checks: true
TypeScript Playground上で作成いたしました。
あなたの回答
tips
プレビュー