大佬教程收集整理的这篇文章主要介绍了你能得到一个基类型的所有子类型的联合吗?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我不知道这在打字稿中是否可行,但我希望能够执行以下操作:
interface Animal {
speed: number,}
interface WingedAnimal extends Animal {
wingspan: number
}
interface MAMMal extends Animal {
gestationPeriod: number
}
type SubtypesUnion<T> = //some deFinition
const animalProps: SubtypesUnion<Animal> // gives you wingedAnimal | MAMMal
这个想法是,如果有 100 个 Animal 亚型,它将是所有亚型的联合。在实践中,我想做如下事情:
interface Order {
material: Material,desTination: DesTination,}
interface Material {
name: String
description: String
}
interface Gears extends Material {
axIEsConfiguration: 'Parallel' | 'IntersecTing'
}
interface SpAnners extends Material {
size: number
}
interface DesTination {
address: String
}
interface Corsborder extends DesTination {
customsFees: number
}
interface Local extends DesTination {
customerPickup: Boolean
}
type FIEldConfig<T> = {
[K in keyof T]: { visible?: Boolean} & {props?: FIEldConfig<T[K]> }
}
const orderFormConfigForEdit: FIEldConfig<Order> = {
material: {
visible: true,props: {
description: {
visible: false,},name: {
visible: true,}
axIEsConfiguration: {
visible: true
}
}
},desTination: {
visible: true,props: {
address: {
visible: false
}
customsFees: {
visible: true
}
customerPickup: {
visible: false
}
}
}
}
问题在于,对于当前的 FIEldConfig
定义,axIEsConfiguration
、customsFees
和 customerPickup
是不允许的。因为它们分别属于 @H_807_5@material 和 DesTination
的子类型。所以我想象了一些像
type FIEldConfig<T> = {
[K in keyof T]: { visible?: Boolean} & {props?: FIEldConfig<SubtypesUnion<T[K]>> }
}
SubtypesUnion
的定义让我无法理解,或者这是不可能的。 Order
是一个域对象,也可用于其他目的。
这是您要找的吗Playground Link
interface Animal {
speed: number,}
interface WingedAnimal extends Animal {
wingspan: number
}
interface MAMMal extends Animal {
gestationPeriod: number
}
type SubtypesUnion<T extends Animal | WingedAnimal> = Animal | WingedAnimal;
const animalProps: SubtypesUnion<Animal> = {
speed: 3
}
以上是大佬教程为你收集整理的你能得到一个基类型的所有子类型的联合吗?全部内容,希望文章能够帮你解决你能得到一个基类型的所有子类型的联合吗?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。