tsdjs / tsd

Check TypeScript type definitions
MIT License
2.39k stars 68 forks source link

`unknown` function parameter passes all type checks #212

Open ehaynes99 opened 6 months ago

ehaynes99 commented 6 months ago

When a function parameter is of type unknown, it is allowed for all type checks. All 3 of the latter checks here should fail:

import * as tsd from 'tsd'

const example1 = (value: number) => String(value)

tsd.expectType<(value: number) => string>(example1) // true positive
tsd.expectType<(value: string) => string>(example1) // true negative
tsd.expectType<(value: Date) => string>(example1) // true negative

const example2 = (value: unknown) => String(value)

tsd.expectType<(value: number) => string>(example2) // FALSE POSITIVE
tsd.expectType<(value: string) => string>(example2) // FALSE POSITIVE
tsd.expectType<(value: Date) => string>(example2) // FALSE POSITIVE

Playground