Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

Typescript allow/return type and its extended childrens

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

  1. allowing union type (ITree | IArrayToTreeInput) or
  2. 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...