可以将类型别名传递给TypeScript中的接口吗?

oknrviil  于 2022-12-05  发布在  TypeScript
关注(0)|答案(1)|浏览(154)

我想写一个TypeScript接口,它是一个类型函数的泛型,而不仅仅是一个类型。

interface Foo<Functor> {
  bar: Functor<string>;
  baz: Functor<number>;
}

其中Functor是我可以从外部传入的某个泛型类型别名。然后我就可以创建不同类型的Foo,如下所示:

type Identity<T> = T;
type Maybe<T> = T | undefined;
type List<T> = T[];

// All of the following would typecheck
const fooIdentity: Foo<Identity> = { bar: "abc", baz: 42 };
const fooMaybe: Foo<Maybe> = { bar: undefined, baz: undefined };
const fooList: Foo<List> = { bar: ["abc", "def"], baz: [42, 43] };

我试图找到一种方法来让编译器接受这一点,但没有运气,所以我想知道是否有一个技巧,我错过了或如果TypeScript只是不能表达这一点。

xj3cbfub

xj3cbfub1#

我迟到了,但现在有一个全面的解决方案,这个问题的用户端,所以我想我可能会分享它。
关于它如何工作的细节可以在here中找到,但简单地说,我们利用了接口可以通过交集接收参数的事实:

type Type = { type: unknown, 0: unknown }

interface $Maybe extends Type { type: this[0] | undefined }

type apply<$T extends Type, V> = ($T & [V])['type']

type Maybe3 = apply<$Maybe, 3> // 3 | undefined

运动场
请注意我们是如何将类型构造函数与其值分离的。
这种模式非常灵活,并支持创建free-types库,该库添加了一系列特性,如支持类型约束、部分应用、组合、可变性、可选性、推理等。
那么,我们可以如何处理您的使用情形呢?
从技术上讲,ListIdentity有现成的类型,我们只需要定义$Maybe即可,也可以将Identity作为Foo的默认参数,这样比较方便。

import { Type, apply, free } from 'free-types';

interface Foo<$T extends Type<1> = free.Id> {
    bar: apply<$T, [string]>;
    baz: apply<$T, [number]>;
}

interface $Maybe extends Type<1> { type: this[0] | undefined }
    
const fooIdentity: Foo = { bar: "abc", baz: 42 };
const fooMaybe: Foo<$Maybe> = { bar: undefined, baz: undefined };
const fooList: Foo<free.Array> = { bar: ["abc", "def"], baz: [42, 43] };

您可能需要检查您的类型构造函数是否实际上可以接受string | number

import { Type, apply, free, Contra } from 'free-types';

interface Foo<$T extends Type<1> & Contra<$T, Type<[string | number]>> = free.Id> {
//                       ------------------- hacked contravariance
    bar: apply<$T, [string]>;
    baz: apply<$T, [number]>;
}
    
const fooIdentity: Foo = { bar: "abc", baz: 42 };
const fooMaybe: Foo<$Maybe> = { bar: undefined, baz: undefined };
const fooList: Foo<free.Array> = { bar: ["abc", "def"], baz: [42, 43] };

// @ts-expect-error: WeakSet expects object, not string | number
type FooWeakSet = Foo<free.WeakSet>;

您可能还需要检查您的类型是否实际上是一个Functor(这里只针对List)。

import { Type, apply, free, Contra } from 'free-types';

interface Foo<$T extends $Functor> {
    bar: apply<$T, [string]>;
    baz: apply<$T, [number]>;
}

type $Functor = Type<1, {
    map: (f: (...args: any[]) => unknown) => unknown
}>

// @ts-expect-error: Identity lacks a map method
const fooIdentity: Foo<free.Id> = { bar: "abc", baz: 42 };

// @ts-expect-error: our Maybe lacks a map method
const fooMaybe: Foo<$Maybe> = { bar: undefined, baz: undefined };

const fooList: Foo<free.Array> = { bar: ["abc", "def"], baz: [42, 43] };

最后,如果你不喜欢有两个版本的相同类型,你可以合并成一个。

import { Type, apply, free, $Alter } from 'free-types';

interface Foo<$T extends Type<1>> {
    bar: apply<$T, [string]>;
    baz: apply<$T, [number]>;
}

type Array<T = never> = $Alter<free.Array, [T]>

type Array1 = Array<1> // 1[]

const fooList: Foo<Array> = { bar: ["abc", "def"], baz: [42, 43] };

运动场

相关问题