Skip to content

Type Alias: IfAny<TypeParameter, YesType, NoType>

IfAny<TypeParameter, YesType, NoType> = 0 extends 1 & TypeParameter ? YesType : NoType

Type utility that detects if a type is 'any' and returns different types accordingly. If T is 'any', returns Y, otherwise returns N.

Uses the fact that 'any' has the special property that 0 extends 1 & any is true.

Type Parameters

TypeParameter

TypeParameter

The type to check for 'any'

YesType

YesType

The type to return if T is 'any'

NoType

NoType

The type to return if T is not 'any'

See

https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360

Example

typescript
type Test1 = IfAny<any, 'is any', 'not any'>        // 'is any'
type Test2 = IfAny<string, 'is any', 'not any'>    // 'not any'
type Test3 = IfAny<unknown, 'is any', 'not any'>   // 'not any'

Released under the MIT License.