Skip to content

null & {} should not simplify to never in a union #28421

Closed
@weswigham

Description

@weswigham

While working on #28394 I was able to identify an issue with inference that was causing us to infer {} for a reverse mapped type's property types - the behavior of {} in an intersection was itself very strange and is what actually led to the problem (otherwise the bad inference would probably never have been observable unless someone was using a this type in their component!).

In short, if I have

interface ReactElement<T> {
    $type: string;
}
type ReactText = string | number;
type ReactChild = ReactElement<any> | ReactText;
interface ReactNodeArray extends Array<ReactNode> { }
type ReactFragment = {} | ReactNodeArray;
type ReactNode = ReactChild | ReactFragment | string | number | boolean | null | undefined;

and I write

declare var a: { children?: ReactNode; };
declare var b: { children?: ReactNode; } & { children?: {} };

a = b;
b = a;
//^^
//Type '{ children?: ReactNode; }' is not assignable to type '{ children?: ReactNode; } & { children?: {} | //undefined; }'.
//  Type '{ children?: ReactNode; }' is not assignable to type '{ children?: {} | undefined; }'.
//    Types of property 'children' are incompatible.
//      Type 'ReactNode' is not assignable to type '{} | undefined'.
//        Type 'null' is not assignable to type '{} | undefined'.

both assignments should work (under strictNullChecks), but they do not!

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefined

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions