Home > Blockchain >  Typescript pulling return type from function interfaces and having issue with pickup wrong type
Typescript pulling return type from function interfaces and having issue with pickup wrong type

Time:07-12

I'm try to extract types from function interface.

...
...
...

type EdmSchemaValue<T> = {
    type: EdmMap<T>;
    nullable?: boolean;
};

type EdmSchema<T> = {[K in keyof T]: T extends object ? EdmSchemaValue<T[K]> : never};

type EdmFunctionSchema<T extends FunctionWithAgs, RT = UnPromisify<ReturnType<T>>> = {
    params: EdmParameterArray<T>;
    returnType: EdmMap<RT> | EdmSchema<RT>;
};

const hostDataSchema: EdmSchema<IHostEntry> = {
    address: {type: Edm.String, nullable: false},
    hostname: {type: Edm.String, nullable: false},
    aliases: {type: [Edm.String], nullable: false},
};

interface IFunc {
    list(all: string): Promise<IHostEntry[]>;
}

const functionSchema: EdmFunctionObject<IFunc> = {
    list: {params: [{type: Edm.String, name: 'all', nullable: false}], returnType: [hostDataSchema]},
};

I think most of this is ok, but I'm getting "hostDataSchema" const hostDataSchema: EdmSchema<IHostEntry> Property 'type' is missing in type 'EdmSchema<IHostEntry>' but required in type 'EdmSchemaValue<IHostEntry>

For this returnType gives (property) returnType: never[] | EdmSchemaValue<IHostEntry>[] which is wrong but I can't figure why as it should be visible either as EdmMap or EdmSchema

Playground

CodePudding user response:

There is a mistake in one of your types:

type EdmFunctionSchema<T extends FunctionWithAgs, RT = UnPromisify<ReturnType<T>>> = {
    params: EdmParameterArray<T>;
    returnType: EdmMap<RT> | EdmSchema<RT>;
};

should be

type EdmFunctionSchema<T extends FunctionWithAgs, RT = UnPromisify<ReturnType<T>>> = {
    params: EdmParameterArray<T>;
    returnType: EdmMap<RT> | EdmSchema<T>; // here
};
  • Related