Skip to content

Using conditional typing to check for extends null does not seem to work correctly #29627

Closed
@not-mike-smith

Description

@not-mike-smith

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms:
Domain: Conditional Types
extends null

Code

type CannotBeNull<T> = {
    readonly [P in keyof T]: T[P] extends NonNullable<T[P]> ? true : false;
};

type DoesntCheckNull<T> = {
    readonly [P in keyof T]: T[P] extends null ? false : true;
};

type MyData = {
	name: string,
	age: number,
	hasChild: boolean,
	childAge: null | number,
	childName: null | string
};

type DataWhichArentNull = CannotBeNull<MyData>;
type DataAreAllTrue = DoesntCheckNull<MyData>;

Expected behavior:
The DoesntCheckNull<T> should have the same result as DataWhichArentNull<T> because it re-uses the logic in NonNullable<T>

i.e. DataWhichAreAllTrue should look like:

{
	name: true,
	age: true,
	hasChild: true,
	childAge: false,
	childName: false,
}

Actual behavior:
DataWhichAreAllTrue actually looks like:

{
	name: true,
	age: true,
	hasChild: true,
	childAge: true,
	childName: true,
}

Playground Link:
playground N.B. turn on --strictNullChecks flag in Options

Related Issues:
Possibly related to 25413.
I thought it was related to 23843, but that was fixed in a PR that was merged 19 days ago, but I could reproduce this issue even when using typescript@next.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions