使用映射类型时,如何在TypeScript中推断对象属性的值?

时间:2018-08-08 15:14:20

标签: javascript typescript

我正在尝试在打字稿中创建一个单例factoryLookup,强制我为联合类型(即映射查找类型)中的每个值定义了工厂,但是我也希望能够推断出查找的任何值的签名(可以是任意参数的值或函数)。这是一个代码示例:

// Using a simple object, I can create a map between a key and an arbitrary function or value
const factoryLookup = {
  foo: (message: string, count: number): string => message + count,
  bar: (): number => 1,
};

// TypeScript can infer function signature of the foo and bar values
factoryLookup.foo('hello world', 3); // foo's signature: (message: string, count: number) => string
factoryLookup.bar(); // bar's signature: () => number

type FactoryTypes = 'foo' | 'bar' | 'baz';

// With a mapped type, TypeScript can enforce I have *something* defined for every member I am mapping
const typedFactoryLookup: { [key in FactoryTypes]: any } = {
  foo: (message: string, count: number) => {},
  bar: () => {},
  baz: (obj: any) => {},
};

// However, I don't know how to infer the mapped values
typedFactoryLookup.foo; // any type
typedFactoryLookup.bar; // any type
typedFactoryLookup.baz; // any type

我希望类型安全,以确保为我的联合/枚举类型中的每个值定义一个函数,还要确保知道该值的签名的类型推断。

任何有关如何解决此问题的想法,或实现相同目标的替代方法,将不胜感激。

1 个答案:

答案 0 :(得分:3)

如果您显式键入一个变量,那么它将是其类型,则不会进行推断。您可以获取所需的行为,但是需要使用函数的推断行为。

from django.db.models import OuterRef, Subquery, Avg, DecimalField

    month_rating_subquery = Rating.objects.filter(
        movie=OuterRef('movie'),
        timestamp__gte=datetime.datetime.now() - datetime.timedelta(days=30)
    ).values('movie').annotate(monthly_avg=Avg('rating'))

    result = Rating.objects.order_by('movie', '-timestamp').distinct('movie').values(
        'movie', 'rating'
    ).annotate(
        monthly_rating=Subquery(month_rating_subquery.values('monthly_avg'), output_field=DecimalField())
    )

您还可以创建一个包含键类型的版本,但是您将需要使用一个返回函数的函数,因为typescript不支持仅指定某些类型参数(在3.1中可能受支持)

type FactoryTypes = 'foo' | 'bar' | 'baz';


function createTypedFactoryLookup<T extends { [key in FactoryTypes]: any }>(factory: T) {
    return factory;
}
const typedFactoryLookup = createTypedFactoryLookup({
    foo: (message: string, count: number) => {},
    bar: () => {},
    baz: (obj: any) => {},
})

typedFactoryLookup.foo; // (message: string, count: number) => void
typedFactoryLookup.bar; // () => void
typedFactoryLookup.baz; // (obj: any) => void