IsNever<T> and IsAny<T>
MediumP3Coding
Implement two helper types (named with a My prefix because IsAny already exists in the test helpers).
MyIsNever<T>:trueifTis exactlynever, otherwisefalse.MyIsAny<T>:trueifTis exactlyany, otherwisefalse.
Both are tricky for the same reason: never and any behave differently from other types in conditional types.
Tests your type must pass
type cases = [
Expect<Equal<MyIsNever<never>, true>>,
Expect<Equal<MyIsNever<never | string>, false>>,
Expect<Equal<MyIsNever<''>, false>>,
Expect<Equal<MyIsNever<undefined>, false>>,
Expect<Equal<MyIsNever<null>, false>>,
Expect<Equal<MyIsNever<[]>, false>>,
Expect<Equal<MyIsNever<{}>, false>>,
Expect<Equal<MyIsNever<any>, false>>,
Expect<Equal<MyIsAny<any>, true>>,
Expect<Equal<MyIsAny<unknown>, false>>,
Expect<Equal<MyIsAny<never>, false>>,
Expect<Equal<MyIsAny<string>, false>>,
Expect<Equal<MyIsAny<undefined>, false>>,
Expect<Equal<MyIsAny<{}>, false>>,
]Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.