'TS strictNullChecks false different behaviour in conditional type

I have the following function:

interface BaseOptions {
  prefix?: string;
}

function foo<T extends BaseOptions>(options?: T): T['prefix'] extends string ? 1 : 2 {
  return 1 as any;
}

When using strictNullChecks: true I get the expected type result:

// strictNullChecks: true
const v1 = foo(); // 2
const v2 = foo({ prefix: 'bar' }) // 1

But when using strictNullChecks: false it's broken:

// strictNullChecks: false
const v12 = foo(); // 1
const v22 = foo({ prefix: 'bar' }) // 1

What's the reason for this behaviour and how can I fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source