
下面的类型声明有误
type Storable = undefined | boolean | number | string | Storable[] | { [key in string]: Storable; } 报错: Index signature is missing in type "XXX" 有好方法能实现这种类型吗?
// 需要满足 let a: Storable a = 5 a = "string" a = [5, "string"] a = { key: { whatever: [ 5, "string", { key: 23 } ] } } // 但不能是 Function 或 Map 或其他千奇百怪的类型 // wrong a = function () { // pass } // wrong a = new Map() // wrong a = { key: new Map() } 1 4ark 2021-06-19 20:49:03 +08:00 ```typescript type Sortable = undefined | boolean | number | string type Sortable2 = Sortable | Sortable[] | Record<string, Sortable> ``` |
2 xiaoming1992 OP |
3 4ark 2021-06-20 11:01:53 +08:00 我才发现其实你的用例是行得通的,递归类型在 typescript 3.7 之后就支持了,详见: https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/#more-recursive-type-aliases 看一下项目使用的 typescript 版本 |