要向解构参数添加类型声明,您需要声明包含对象的类型。
来自typescript documentation:
... 令人困惑的是,这里的冒号并不表示类型。类型,如果指定的话,还是需要在整个解构之后写...
let { a, b }: { a: string, b: number } = o;
关于深度嵌套解构的 PSA:
谨慎使用解构。正如前面的示例所展示的,除了最简单的解构表达式之外的任何东西都令人困惑。对于深度嵌套的解构尤其如此,即使没有重命名、默认值和类型注释,也很难理解。 尽量保持解构表达式的小而简单。您始终可以编写解构将自己生成的赋值。
解构函数参数
在函数中,您可以这样声明解构参数的类型:
export default ({ a, b }: {a: string, b: number}) => (
...
);
但是,在更长的示例中,这看起来很糟糕:
export default ({
input: { name, onChange, value, ...restInput },
meta,
...rest
}: {
input: {
name: string, onChange: ()=>void, value:string, ...restInput
}, meta: object
}) => (
...
);
看起来很糟糕,所以在这里你能做的最好的事情就是为你的参数声明一个接口并使用它而不是内联类型:
interface Params {
input: {
name: string;
onChange: ()=>void;
value: string;
};
meta: object;
}
export default ({
input: { name, onChange, value, ...restInput },
meta,
...rest
}: Params) => {};
Playground
Article with much more
休息参数
对于其余参数,根据您对这些类型的期望,您可以使用index signature:
interface Params {
// This needs to match the other declared keys and values
[key: string]: object;
input: {
[key: string]: string | (() => void);
name: string;
onChange: ()=>void;
value: string;
};
meta: object;
}
export default ({
input: { name, onChange, value, ...restInput },
meta,
...rest
}: Params) => { };
这会给...rest 一个类型{[key: string]: object} 例如。
Rest parameter playground