この解答例はChatGPTによって生成されています,正しいかは自己判断で。 皆の投稿からも解答例が見つかるかもしれませんよ。
BFE.devでのAI解答例 - TypeScript タイプパズル
49. asserts never
type Value = 'a' | 'b' | 'c';function assertsNever(_: never): never { throw new Error('Unexpected value. Should have never happened.');}declare let value: Value;switch (value) { case 'a': break; case 'b': break; default: assertsNever(value); break;}
We declare a function assertsNever()
that takes an argument of type never
and always throws an error. This function is called in the default
case of the switch
statement to check for exhaustiveness.
If we add a new case to the Value
type and forget to update the switch
, TypeScript will now complain because we have called assertsNever()
in the default
case.