[Typescript] Recursion Type


Recursive types, are self-referential, and are often used to describe infinitely nestable types. For example, consider infinitely nestable arrays of numbers

[3, 4, [5, 6, [7], 59], 221]

You may read or see things that indicate you must use a combination of interface and type for recursive types. As of TypeScript 3.7 this is now much easier, and works with either type aliases or interfaces.

type NestedNumbers = number | NestedNumbers[]
 
const val: NestedNumbers = [3, 4, [5, 6, [7], 59], 221]

 

原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/277363.html

(0)
上一篇 2022年7月27日 20:04
下一篇 2022年7月27日 20:04

相关推荐

发表回复

登录后才能评论