Skip to content

Type of generic function parameter not being inferred from interface generic type. #30505

Closed
@pikax

Description

@pikax

TypeScript Version: 3.4.0@rc

Search Terms:

Code

export type Prop<T> = { (): T }
export type PropType<T> = Prop<T>;
export type PropDefaultValue<T> = T;


export type PropValidatorFunction<T> = (value: T) => boolean;
export type PropValidator<T> = PropOptions<T>;


export type PropOptions<T> = {
    type: PropType<T>;

    value?: PropDefaultValue<T>,
    required?: boolean;
    validator?: PropValidatorFunction<T>;
}

export type RecordPropsDefinition<T> = {
    [K in keyof T]: PropValidator<T[K]>
}
export type PropsDefinition<T> = RecordPropsDefinition<T>;


declare function extend<T>({ props }: { props: PropsDefinition<T> }):  PropsDefinition<T>;

interface MyType {
    valid: boolean;
}

const r = extend({
    props: {
        // gets resolved to PropOptions<any> but should be PropType<MyType>
        notResolved: {
            type: Object as PropType<MyType>,

            // type not inferred 
            validator: (x) => {
                return x.valid;
            }
        },
        // gets resolved currectly to PropType<MyType>
        // on [email protected] doest resolve to PropType<MyType>
        explicit: {
            type: Object as PropType<MyType>,

            // type not inferred 
            validator: (x: MyType) => {
                return x.valid;
            }
        }
    }
})

// return type on 3.4@rc doesn't get resolved
r.explicit
r.notResolved
r.explicit.required
r.notResolved.required

Expected behavior:
The validator parameter as typescript type inferred from T.
Props record values to be resolved with the interface of PropType
Return type to be PropsDefinition<T>

Actual behavior:
Getting validator parameter resolved as any/unknown.
Record types are not getting resolve to the type PropOptions<T> in 3.4@rc but working in 3.3.x
The return type not working in 3.4@rc

Playground Link:
playground

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions