在 TypeScript 中键入匿名对象的属性
Typing the properties of an anonymous object in TypeScript
我有一个函数:
function someFunction({ propertyA, propertyB })
{
return 1;
}
我想为函数参数中的匿名对象中的属性提供一个显式类型,但是为此使用典型的 TypeScript 语法 (propertyA: boolean
) 会导致将类型放在通常值所在的位置因为对象字面量中的 :
表示 'the property on the left has the value on the right'.
我想知道我该怎么做?编译器向我发出有关隐式具有 any
类型的属性的警告。抱歉,这可能是非常基本的,但我用 Google 搜索并查看了这个网站,但找不到任何解决这个特定问题的方法。
function someFunction({ propertyA, propertyB }: {propertyA: boolean; propertyB: number }){
//...
或者,更好的是:明确类型:
interface SomeFunctionOpts{
propertyA: boolean;
propertyB: number;
}
function someFunction({ propertyA, propertyB }: SomeFunctionOpts) {
//...
我有一个函数:
function someFunction({ propertyA, propertyB })
{
return 1;
}
我想为函数参数中的匿名对象中的属性提供一个显式类型,但是为此使用典型的 TypeScript 语法 (propertyA: boolean
) 会导致将类型放在通常值所在的位置因为对象字面量中的 :
表示 'the property on the left has the value on the right'.
我想知道我该怎么做?编译器向我发出有关隐式具有 any
类型的属性的警告。抱歉,这可能是非常基本的,但我用 Google 搜索并查看了这个网站,但找不到任何解决这个特定问题的方法。
function someFunction({ propertyA, propertyB }: {propertyA: boolean; propertyB: number }){
//...
或者,更好的是:明确类型:
interface SomeFunctionOpts{
propertyA: boolean;
propertyB: number;
}
function someFunction({ propertyA, propertyB }: SomeFunctionOpts) {
//...