简体   繁体   English

即使类型不匹配,打字稿也没有错误

[英]typescript no error even though type is not matched

type path= {
    col(...args: any[]): string
    doc(...args: any[]): string
}

export const creator =  <
    C extends path['col'],
    D extends path['doc']
>(pathGenerator: {
    col: C
    doc: D
}) => pathGenerator

export const profile = creator({
    col({ userId }:{ userId: string }) {
        return 'user' + '/' + userId
    },
    doc({ userId, profileId }: { userId: string, profileId: string }) {
        return this.col() + '/' + profileId // doesnt error eventhough some member is missing in this.col argument
    },
})

profile.col() // error shown correctly

consider this piece of code考虑这段代码

there is no error even though in the tooltip it clearly needs an argument即使在工具提示中它显然需要一个参数,也没有错误

在此处输入图片说明

however, the error is shown correctly if we try to access col via profile但是,如果我们尝试通过配置文件访问 col,则会正确显示错误

在此处输入图片说明

playground 操场

questions:问题:

  1. why is this happening?为什么会这样?
  2. how to solve this?如何解决这个问题?

Most of the time I see the error that you posted, but if I hover just right I see the generic definition with (...args: any[]) .大多数时候我会看到您发布的错误,但是如果我将鼠标悬停在正确的位置,我会看到带有(...args: any[])的通用定义。

在此处输入图片说明

Both col and doc are functions which return a string . coldoc都是返回string函数。 If I use the generic to designate the args rather than the whole function, everything works as expected and I get an error.如果我使用泛型来指定 args 而不是整个函数,一切都按预期工作,我会收到错误消息。 Does that work for you?那对你有用吗?

export const creator =  <
    C extends {},
    D extends {}
>(pathGenerator: {
    col: (props: C) => string,
    doc: (props: D) => string
}) => pathGenerator

export const profile = creator({
    col({ userId }:{ userId: string }) {
        return 'user' + '/' + userId
    },
    doc({ userId, profileId }: { userId: string, profileId: string }) {
        return this.col() + '/' + profileId // shows error!
    },
})

Playground Link 游乐场链接

暂无
暂无

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

相关问题 Typescript编译错误,即使参数的类型正确 - Typescript compile error even though parameters are of the correct type 打字稿“TS2339:属性 &#39;X&#39; 不存在于类型 &#39;Y&#39;”错误,即使类型已定义 - TypeScript "TS2339: Property 'X' does not exist on type 'Y'" error even though type is defined Typescript 显示错误,即使它在 VSCode 上编译也是如此 - Typescript shows an error even though it compiles on VSCode 导入“未导出”类型时出现打字稿错误,即使它已导出 - Typescript errors when importing a "not exported" type, even though it is exported 为什么即使代码正在检查未定义,typescript 仍会返回此编译错误? - why is typescript returning this compile error even though the code is checking for undefined? Typescript-即使定义了所有选项,Type仍缺少type的以下属性 - Typescript - Type is missing the following properties from type even though all options are defined 为什么 typescript 在 reduxToolkit createSlice function 中抱怨 redux 切片的 initialState 类型,即使它是正确的类型? - Why typescript complains about redux slices' initialState type in reduxToolkit createSlice function, even though it is correct type? TypeScript 永远不会在条件类型中输入不一致匹配? - TypeScript never type inconsistently matched in conditional type? 即使类型正确,Typescript 也会为错误的类型抛出错误 - Typescript throwing an error for the wrong type even when the type is right 即使返回的类型是对象,也无法访问对象属性 - React + Typescript - Can't access object property even though type of returns it is an object - React + Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM