typescript - 打字稿中所有对象文字类型的子类型?
问题描述
假设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
应该可以使用 form 分配给所有可能的情况,有无限可能的有效值Func<{x: 1, y: string}>
,我目前只能找到. 并对是否存在更受约束的人感到好奇。any
U
解决方案
U
需要是包含所有其他类型的类型,因此是所有类型的交集:
let a: Func<{p1: string, p2: number, p3: number, x: boolean, x1: number}>
推荐阅读
- java - 打开新窗口时未加载 FXML 标签值
- oracle - 如何在 oracle select 语句中获取列名
- sonarqube - Gitlab SonarQube 管道通知电子邮件
- java - 访问 JPA LifecycleEvent 中的非映射属性
- android - 新的 Google 权限政策:READ_SMS 和类似政策
- java - CompletableFuture.supplyAsync() 中 ForkJoinPool 的行为
- mysql - 如何以正确的方式创建具有相似数据的表?
- jenkins - 使用 groovy 脚本自动化 Jenkins Keycloak 插件
- c - 如何以正确的方式将结构的数组传递给我的函数?
- aws-appsync - 我可以将自定义嵌套 CloudFormation 堆栈添加到 AWS Amplify 还是可以使用 GraphQL/AppSync 创建自定义 Lambda 函数/SQS?