number | undefined > ( left: number | undefined, right: number | undefined, ): Left extends number ? number : Right extends number ? number : number | undefined { return left ?? right as any; } 10
number | undefined > ( left: number | undefined, right: number | undefined, ): Left extends number ? number : Right extends number ? number : number | undefined { return left ?? right as any; } 11 LeftかRightがnumberなら 返り値はnumber
number | undefined > ( left: number | undefined, right: number | undefined, ): Left extends number ? number : Right extends number ? number : number | undefined { return left ?? right as any; } 12 TSの新機能の力で asの使用を消したい!
number | undefined > ( left: number | undefined, right: number | undefined, ): Left extends number ? number : Right extends number ? number : number | undefined { return left ?? right as any; } 15 条件を満たしていない • 複数の型引数が条件に 含まれている • 最後がneverではない
number | undefined > ( left: number | undefined, right: number | undefined, ): Right extends number ? number : Right extends number | undefined ? Left : never { return left ?? right as any; } 16 • Rightだけで条件を記述 • 最後をneverにした
number | undefined > ( left: number | undefined, right: number | undefined, ): Right extends number ? number : Right extends number | undefined ? Left : never { if (right !== undefined) return left ?? right; return left; } 18
number | undefined > ( left: number | undefined, right: number | undefined, ): Right extends number ? number : Right extends number | undefined ? Left : never { if (right !== undefined) return left ?? right; return left; } 19 条件型の分岐と同じように rightで分岐することで 型チェックが通った