首页 > 解决方案 > 将对象文字分配给打字稿泛型类型

问题描述

我正在构建一个接口,其中我将拥有一个带有任意键的对象,每个键都是实现接口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

这是上面的打字稿游乐场链接

标签: typescripttypescript-generics

解决方案


简短回答:不,您不能分配包含异构类型的对象文字维护泛型类型约束。需要一个受约束的辅助函数(如当前实现的那样)。

扩展接口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尝试访问. 但是,在 的上下文中,引用 的实例会导致错误,因为类型已缩小为。entriesSimpleConditiontype FormConditions<E, T extends Record<string, Condition>>ListConditionentriesCondition

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)

推荐阅读