Skip to content

Predicate function generated by isOneOf<T> narrowed to invalid type in some case #49

@lambdalisue

Description

@lambdalisue

The predicate function should narrowed to Foo | Bar but it actually narrowed to Foo in the following case

type Predicate<T> = (x: unknown) => x is T;
type Foo = {
    foo: string;
};
type Bar = {
    foo: string;
    bar?: string;
};
type F = Predicate<Foo>;
type B = Predicate<Bar>;

// Please fix this definition.
type OneOf<T extends Predicate<unknown>[]> = T extends Predicate<infer U>[] ? U : never;

const a: OneOf<[F, B]> = {foo: "", bar: ""}; // `a` is not `Foo | Bar` but `Foo` so this assignment fails.

I'm not sure if this is TypeScript limitation but you can play this with TS Playground.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions