[英]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,则会正确显示错误
questions:问题:
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
. col
和doc
都是返回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!
},
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.