简体   繁体   English

为什么ts在声明变量的时候不调用function,提示类型错误

[英]Why doesn't ts call the function when declaring a variable, indicating a type error

const fn1 = (arg1: {
    key: number,
})=>{
    console.log(arg1)
}

fn1({
 key: 1
})

const data = {
    key: 1,
    a: 1,
}
fn1(data)

fn1({
    key: 1,
    a: 1,
})
  • link 关联
  • who can give some help: Why doesn't ts call the function when declaring a variable, indicating a type error谁能给点帮助:为什么ts在声明变量的时候不调用function,提示类型错误
const fn1 = (arg1: {
    key: number,
})=>{
    console.log(arg1)
}

here you declare that the parameter should have a key named "key" and a value as a number, nothing more.在这里你声明参数应该有一个名为“key”的键和一个数字值,仅此而已。 if you want to create a type for a parameter that should expect any any key with a number value pair, you need to change it as如果你想为一个参数创建一个类型,该类型应该期望任何带有数字值对的键,你需要将它更改为

const fn1 = (arg1: {
    [k: string]: number
})=>{
    console.log(arg1)
}

To solve the error解决错误

As the error message suggest:如错误消息所示:

Object literal may only specify known properties, and 'a' does not exist in type '{ key: number; }'

It is the difference between object literal and a named object.它是 object 文字和命名 object 之间的区别。

However, you can still make your function generic to make it compiles.但是,您仍然可以使 function 通用以使其编译。

const fn1 = <T extends {key: number}>(arg1: T)=>{
    console.log(arg1)
}

Edit编辑

Maybe your question is why, but not how.也许您的问题是为什么,而不是如何。 It is acutally a feature introduced in TypeScript 1.6 to catch programmer human errors on misspelling the property names.它实际上是TypeScript 1.6 中引入的一个功能,用于捕获程序员在拼写错误属性名称时的人为错误。

You can turn on suppressImplicitAnyIndexErrors in your tsconfig if you prefer to ignore it.如果您愿意忽略它,可以在 tsconfig 中打开suppressImplicitAnyIndexErrors But it is generally not recommended by typescript.但是typescript一般不推荐

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 声明变量类型时看似无关的错误 - Seemingly unrelated error when declaring variable type 为什么将“ any”类型的对象设置为具有特定类型的属性不会引发TS编译错误? - Why setting object of “any” type to property with specific type doesn't throw TS compilation error? 条件语句中类型推断的 TS 错误,当条件使用函数调用的结果而不是布尔表达式时 - TS Error with Type Inference within Conditional Statement, when the condtional is using the result of a function call instead of a boolean expression 为什么 TypeScript 在重新声明变量时没有推断出正确的类型 - Why TypeScript doesn't infer the correct type when redeclaring a variable 为什么箭头 function 的隐含“返回”在这种情况下不起作用(TS)? - Why implied `return` of arrow function doesn't work in this case (TS)? 为什么 obj[&#39;NonExistentKey&#39;] 不在 TS 中抛出错误? - Why doesn't obj['NonExistentKey'] throw an error in TS? TypeScript:用回调 function 覆盖 Map 作为值,当它的参数是更具体的类型不起作用(TS2416) - TypeScript: overriding a Map with callback function as value when its argument is of more specific type doesn't work (TS2416) TS2339-明显有效的TS文件中的“类型上不存在属性”错误 - TS2339 - 'Property doesn't exist on type' error in an apparently valid TS file 在 function 调用中声明 object 的类型? - Declaring type of an object inside a function call? 返回与类型别名不匹配的函数不会产生错误-为什么? - returning a function that doesn't match type alias doesn't produce an error - why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM