Skip to content

unknown[] as rest params with generic parameter breaks generic inference #27439

Closed
@skeate

Description

@skeate

TypeScript Version: 3.1.0

Search Terms: unknown conditional rest params

Code

declare function f<T>(x: T): T;
declare function g<T>(x: T): number;

//shortened builtin: type ReturnType<F> = F extends (...args: any[]) => infer U ? U: any
type ReturnOf<F> = F extends (...args: unknown[]) => infer U ? U : never

declare const a: ReturnType<typeof g>; // number
declare const b: ReturnOf<typeof g>; // number
declare const c: ReturnType<typeof f>; // {}
declare const d: ReturnOf<typeof f>; // never

Expected behavior: d is the same as c (i.e. type {})

Actual behavior: d is type never

Playground Link

Related Issues: didn't see any; apologies if I missed it

Notes: this only happens if there's a T-typed (or union with T) parameter. Even something like Promise<T> seems to cause it to evaluate to {}. I'm not even 100% sure this is a bug (I might be missing/misunderstanding something in how unknown is handled), but it seems like in this situation it should behave the same as the any version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions