Answer the question
In order to leave comments, you need to log in
How to write a TypeScript function?
let a: string[] | CustomClass[];
test(a);
function test(data: string[]) {
//Some code
}
Argument of type 'string[] | 'CustomClass[]' is not assignable to parameter of type 'string[]'.
Answer the question
In order to leave comments, you need to log in
Something like this can be done:
type TestResult<T extends string[] | CustomClass[]> = T extends CustomClass[] ? T : void;
function test<T extends string[] | CustomClass[]>(data: T): TestResult<T> {
if (data[0] instanceof CustomClass) {
return data as TestResult<T>;
}
return undefined as TestResult<T>;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question