8000 TypeScript unexpectedly inferring 'any' type. · Issue #30250 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
TypeScript unexpectedly inferring 'any' type. #30250
Closed
@CyrusNajmabadi

Description

@CyrusNajmabadi

Given the following code:

interface Widget { x: number }

declare type Unwrap<T> = T extends Promise<infer U1> ? UnwrapSimple<U1> : UnwrapSimple<T>;
declare type primitive = Function | string | number | boolean | undefined | null;

declare type UnwrapSimple<T> = T extends primitive ? T : T extends Array<infer U> ? UnwrappedArray<U> : T extends object ? UnwrappedObject<T> : never;

export interface UnwrappedArray<T> extends Array<Unwrap<T>> {
}

export declare type UnwrappedObject<T> = {
    [P in keyof T]: Unwrap<T[P]>;
};

declare var w: Widget;
declare type TupleType = [Widget, string];
declare var o: UnwrappedObject<TupleType>;

TypeScript oddly infers a type of [any, string] for o. This is surprising to us given how we would think these type operators would work. First, given the definition UnwrappedObject i would expect UnwrappedObject<TupleType> to expand like so:

UnwrappedObject<TupleType>  ->
UnwrappedObject<[Widget, string]> ->
[Unwrap<Widget>, Unwrap<string>] ->
[UnwrappedObject<Widget>, string] ->
[{ x: Unwrap<number> }, string] ->
[{ x: number }, string]

HOwever, instead of getting { x: number } (which is just the Widget type), we end up with 'any'. Cany anyone shed any light on this? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0