typescript - 将对象文字分配给打字稿泛型类型
问题描述
我正在构建一个接口,其中我将拥有一个带有任意键的对象,每个键都是实现接口FormConditions
的类的实例。Condition
我想将此对象文字分配给一个变量,并让结果对象的类型 A) 仅响应对象文字中的键,并且 B) 尊重这些键中的每一个可能具有的任何子类化或其他扩展。
如果您检查下面的代码,我会发现所有实际类型都可以正常工作。我遇到的问题是我不知道如何在不明确声明每个键的子类型的情况下直接将对象分配给变量。相反,我可以通过makeFormConditions
使用泛型正确推断结果对象的类型的标识函数传递它。这是执行此操作的唯一方法还是有直接分配它的方法?随意更改FormCondition
您认为合适的定义以完成此操作。
interface Condition {
name: string
id: number
}
type FormConditions<T extends Record<string, Condition>> = {
[P in keyof T]: T[P]
}
class SimpleCondition implements Condition {
constructor(public name: string, public id: number) {}
}
class ListCondition<T> implements Condition {
constructor(public name: string, public id: number, public entries: T[]) {}
}
// This is a passthrough function just to make the types work
function makeFormConditions<T extends Record<string, Condition>>(obj: T): FormConditions<T> {
return obj;
}
// Would prefer to avoid the function call to make types work
const conditions = makeFormConditions({
simpleOne: new SimpleCondition('simpleOne', 1),
simpleTwo: new SimpleCondition('simpleTwo', 2),
list: new ListCondition('list', 3, ['foo', 'bar'])
})
// This works but is redundantly verbose
// const conditions : FormConditions<{
// simpleOne: SimpleCondition;
// simpleTwo: SimpleCondition;
// list: ListCondition<string>;
// }> = {
// simpleOne: new SimpleCondition('simpleOne', 1),
// simpleTwo: new SimpleCondition('simpleTwo', 2),
// list: new ListCondition('list', 3, ['foo', 'bar'])
// }
//
// would instead prefer to not use the function or be
// really specific about the type declaration:
// const conditions : FormConditions = {
// simpleOne: new SimpleCondition('simpleOne', 1),
// simpleTwo: new SimpleCondition('simpleTwo', 2),
// list: new ListCondition('list', 3, ['foo', 'bar'])
// }
conditions.list.name
conditions.list.entries
conditions.simpleOne.name
conditions.simpleOne.entries // error, as expected
这是上面的打字稿游乐场链接。
解决方案
简短回答:不,您不能分配包含异构类型的对象文字并维护泛型类型约束。需要一个受约束的辅助函数(如当前实现的那样)。
扩展接口Condition
以包含所有子类型属性
的定义Condition
可以扩展为接受一个可选entries
数组,这样FormConditions<E, T extends Record<string, Condition<E>>>
可以同时包含SimpleConditions
和ListConditions
。这具有不希望的副作用,即实例SimpleCondition
可能会引用缺失的entries
属性而不会出错。
interface Condition<E> {
name: string
id: number
entries?: E[]
}
type FormConditions<E, T extends Record<string, Condition<E>>> = {
[P in keyof T]: T[P]
}
class SimpleCondition<E = never> implements Condition<E> {
constructor(public name: string, public id: number) {}
}
class ListCondition<E> implements Condition<E> {
constructor(public name: string, public id: number, public entries: E[]) {}
}
const conditions: FormConditions<string, Record<string, Condition<string>>> = {
simpleOne: new SimpleCondition('simpleOne', 1),
simpleTwo: new SimpleCondition('simpleTwo', 2),
list: new ListCondition('list', 3, ['foo', 'bar'])
}
conditions.list.name;
conditions.list.entries;
conditions.simpleOne.name;
conditions.simpleOne.entries; // Expected error; however, no error, since `entries` is optional parameter.
将接口限制Condition
为仅包含name
和id
由于受到限制,因此在Condition
尝试访问. 但是,在 的上下文中,引用 的实例会导致错误,因为类型已缩小为。entries
SimpleCondition
type FormConditions<E, T extends Record<string, Condition>>
ListCondition
entries
Condition
interface Condition {
name: string
id: number
}
type FormConditions<E, T extends Record<string, Condition>> = {
[P in keyof T]: T[P]
}
class SimpleCondition<E = never> implements Condition {
constructor(public name: string, public id: number) {}
}
class ListCondition<E> implements Condition {
constructor(public name: string, public id: number, public entries: E[]) {}
}
const conditions: FormConditions<string, Record<string, Condition>> = {
simpleOne: new SimpleCondition('simpleOne', 1),
simpleTwo: new SimpleCondition('simpleTwo', 2),
list: new ListCondition('list', 3, ['foo', 'bar'])
}
conditions.list.name;
conditions.list.entries; // Error: Property 'entries' does not exist on type 'Condition'.
conditions.simpleOne.name;
conditions.simpleOne.entries; // error (as expected - Good)
推荐阅读
- r - 如何指定 sf::st_sample 中点之间的最小距离?
- javascript - React Native,redux 功能在更新后不会恢复到原始状态
- python - 如何找到 IPython 单元格的宽度?
- python - 关于使用 Python scipy 对信号进行反卷积的问题
- bash - 不确定如何正确使用变量并在 bash 脚本中的 if 语句中使用它们
- reactjs - 如何在 React 中显示来自全局状态的数据?
- api - API 认证令牌
- angular - 以角度 8 将德语区域设置数字格式 (0,01) 转换为英语区域设置格式 (0.01)
- python - 使用 opencv 加载使用 tifffile 保存的图像
- java - 在 Sax Parser JAVA 中创建层次结构