将对象文字分配给Typescript泛型类型

时间:2019-08-29 21:04:51

标签: typescript typescript-generics

我正在构建一个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

在上面是typescript playground link

1 个答案:

答案 0 :(得分:1)

简短的回答::不能,您不能分配包含异构类型的对象文字, 并且 保持通用类型约束。需要受约束的辅助函数(当前已实现)。

扩展界面Condition以包括所有子类型属性

Condition的定义可以扩展为接受可选的entries数组,这样FormConditions<E, T extends Record<string, Condition<E>>>可以同时容纳SimpleConditionsListConditions。这具有不希望的副作用,即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仅包含nameid

由于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)
相关问题