Answer the question
In order to leave comments, you need to log in
How to fix an iterator bug with the spread operator if the function can return an array or an object?
There is a function that can return an array or an object:
public static getSymbols(getSymbolsOnly = false): string[] | { symbols: string[], group: string } {
const symbols = ['Symbol 1', 'Symbol 2'];
if (getSymbolsOnly) {
return symbols;
}
return {
symbols: symbols,
group: 'Group name',
}
}
const symbols = InstrumentsService.getSymbols(true);
assetSymbols = [
...symbols
]
Answer the question
In order to leave comments, you need to log in
https://www.typescriptlang.org/play?#code/JYOwLgpg...
interface SomeType {}
class SomeClass {
static getSymbols(): SomeType;
static getSymbols(getSymbolsOnly: true): string[];
static getSymbols(getSymbolsOnly: false): SomeType;
static getSymbols(getSymbolsOnly: boolean = false): string[] | SomeType {
if(getSymbolsOnly) {
return [];
}
return {};
}
}
const t1 = SomeClass.getSymbols(); // SomeType
const t2 = SomeClass.getSymbols(true); // string[]
const t3 = SomeClass.getSymbols(false); // SomeType
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question