简体   繁体   English

打字稿中所有对象文字类型的子类型?

[英]Subtype of all object literal types in typescript?

assume strictFunctionTypes is turned on, so that function params are checked contraviariantly假设strictFunctionTypes已打开,以便逆向检查函数参数

interface Func<T> {
  (p: T): unknown
}
declare let b: Func<{p1: string}>
declare let c: Func<{p2: number}>
declare let d: Func<{p3: number, x: boolean, x1: number}>
// e, f, g... like before
let a: Func<U> // to make b, c, d, e, f... etc all assignable to a, what should U be
a = b
a = c
a = d

a should be assignable to all possable cases with form Func<{x: 1, y: string}> , there are infinity possible number of valid values, I currently can only find any as a valid value for U . a应该可以分配给形式为Func<{x: 1, y: string}>所有可能的情况,有效值的数量有无穷多个,我目前只能找到any作为U的有效值。 And curious about the existence of a more constrained one.并且对一个更受约束的存在感到好奇。

U需要是一个包含所有其他类型的类型,所以是所有类型的交集:

let a: Func<{p1: string, p2: number, p3: number, x: boolean, x1: number}>

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

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