Closed
Description
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!