简体   繁体   English

为什么在 TypeScript 中将缺少类型参数推断为未知?

[英]Why are missing type parameters inferred as unknown in TypeScript?

Why does a generic type parameter in a call get inferred to the unknown type (or the constraint type) if it is omitted.如果省略,为什么调用中的泛型类型参数会被推断为unknown类型(或约束类型)。 Consder,康德,

function doStuff<T>(): T {
  return {} as any as T;
}

const result = doStuff();

I would expect the call of doStuff to be an error as the type parameter is missing.我希望doStuff的调用是一个错误,因为缺少类型参数。 Instead it infers unknown so the type of result is unknown .相反,它推断为unknown所以result的类型是unknown Why?为什么? If T has a constraint then the type of result is the constraint type.如果 T 有一个约束,那么result的类型就是约束类型。

I can understand defaults being useful but TypeScript has a defaulting mechanism for generic parameters.我可以理解默认值很有用,但 TypeScript 具有通用参数的默认机制。 Is this an historical hang-up or what is the thinking?这是一个历史性的挂断还是什么想法?

I'm using TypeScript 3.9.我正在使用 TypeScript 3.9。

This is a similar quesition to this but I am asking the question why (not assuming it is incorrect) and this example is simplier.这是一个与此类似的问题,但我问的是为什么(不假设它不正确),这个例子更简单。

It's documented in the (increasingly outdated) TypeScript Spec here that "if the set of candidate argument types is empty, the inferred type argument for T is T 's constraint."在(越来越过时的)TypeScript 规范中记录“如果候选参数类型集为空,则推断的T类型参数是T的约束。” So it's intended, but you want to know why .所以这是有意的,但你想知道为什么 I assume you don't want my opinion about why it might be useful behavior not to issue an error instead, so the best I can do is see if there's any documented discussion by the TS team about this.我假设您不希望我的意见关于为什么不发出错误可能是有用的行为,所以我能做的最好的事情就是看看 TS 团队是否有任何记录在案的讨论。

Minor aside: In TypeScript 3.5, the implicit constraint for generic type parameters was changed from {} to unknown .次要问题:在 TypeScript 3.5 中,泛型类型参数的隐式约束{}更改为unknown That's just some context for the only issues I can find about this:这只是我能找到的唯一问题的一些背景:

There's microsoft/TypeScript#360 , asking for an error to be issued if type inference produces {} .microsoft/TypeScript#360 ,如果类型推断产生{}则要求发出错误。 The issue description talks mostly about the situation when there are multiple non-overlapping type inference candidtates and the compiler widens all the way to the top-like type in order to find a "best common supertype".问题描述主要讨论存在多个非重叠类型推断候选者并且编译器一直扩大到类似顶部的类型以找到“最佳通用超类型”的情况。 This is not your problem, and this issue has already been addressed in TypeScript by using unions and preventing union inference if the types are unrelated.这不是您的问题,并且此问题已在 TypeScript 中通过使用联合并在类型不相关时防止联合推断得到解决。

But in this comment , the discussion switches to what to do when there are no candidates.但在这条评论中,讨论切换到没有候选人时该怎么办。 Should it be an error?应该是错误吗? Aaand, that's about as much as I can find in that issue. Aaand,这就是我在该问题中所能找到的。 The issue was resolved without erroring on an empty set of candidates, and the suggestion was closed as "declined".该问题得到解决,没有在一组空的候选人上出错,并且该建议被关闭为“拒绝”。

There's also microsoft/TypeScript#2511 which asks for this again.还有microsoft/TypeScript#2511再次要求这个。 It's mentioned in this comment as option 2: "Give an error when {} is inferred because there are no inference candidates. We have discussed this option before, and it never really gained much traction, but we could revisit."它在此评论中作为选项 2 提到:“在推断{}时给出错误,因为没有推理候选者。我们之前已经讨论过这个选项,但它从未真正获得太多关注,但我们可以重新审视。” And maybe some work was even done on it in a now-deleted branch called downWithDreadedCurlyCurly .也许甚至在一个现已删除的名为downWithDreadedCurlyCurly的分支中完成了一些工作。 But the issue ends up getting closed since a tangentially related issue fixes part of this (using contextual types to add a candidate from return types).但是这个问题最终会被关闭,因为一个切向相关的问题修复了部分问题(使用上下文类型从返回类型中添加候选者)。

And finally there's microsoft/TypeScript#5254 , asking for this again, some discussion goes back and forth, and nothing comes of it.最后是microsoft/TypeScript#5254 ,再次要求这个,一些讨论来回进行,但没有任何结果。

So, that's it.就是这样了。 It's intended;这是有意的; some people have thought it should be different a while ago;有些人不久前认为应该有所不同; the idea didn't get much traction;这个想法没有得到太多的关注; it was abandoned.它被遗弃了。 None of this really says why the current behavior is preferred;这些都没有真正说明为什么首选当前行为。 the answer may well be inertia;答案很可能是惯性; it works well enough for people and those who had a problem with it had their problem addressed in other ways.它对人们来说效果很好,那些有问题的人可以通过其他方式解决他们的问题。

If someone can find a more canonical answer to this, I'd be interested in seeing it.如果有人能找到一个更规范的答案,我会很想看到它。 Good luck!祝你好运!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM