tristantzara
tristantzara

Reputation: 5917

Typescript conditional types seem to allow null

Given this example of conditional type:

type MyType = 'Number' | 'String';

interface Test<T extends MyType> {
  bar: T extends 'Number' ? 25 : '25'
}

If I try to:

const test: Test<'Number'> = {bar: null}

I get no complains, this seems to be valid Typescript. However, when I hover over bar I get a popup saying (property) Test<"Number">.bar: 25 which seems to mean that TS has understood the type correct.

Why do I get no error assigning in such a way then? How can I fix that?

Upvotes: 0

Views: 92

Answers (1)

Paleo
Paleo

Reputation: 23772

Just enable the compiler option --strict or --strictNullChecks.

Upvotes: 2

Related Questions