export interface IArrayToTreeInput {}
export function convertArrayToTree(input: IArrayToTreeInput[]) {
const roots: IArrayToTreeInput[] = [];
return roots;
}
export interface ITree extends IArrayToTreeInput {}
let tree: ITree[] = [];
tree = convertArrayToTree(tree);
Last line will throw error. as ITree has more values than IArrayToTreeInput. Now I can solve it by
- allowing union type (ITree | IArrayToTreeInput) or
- specifying as ITree (convertArrayToTree(tree) as ITree).
But is there a way where the function can return extended type without either of the above 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…