gpt4 book ai didi

typescript - 您可以将类型别名传递给 TypeScript 中的接口(interface)吗?

转载 作者:行者123 更新时间:2023-12-05 06:06:24 26 4
gpt4 key购买 nike

我想编写一个 TypeScript 接口(interface),该接口(interface)在类型函数上是通用的,而不仅仅是一个类型。换句话说,我想写一些像

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 是否无法表达这个。

最佳答案

我来晚了,但现在在用户端有一个全面解决这个问题的方法,所以我想我可以分享一下。

有关其工作原理的详细信息,请参见 here ,但简而言之,我们利用接口(interface)可以通过交集接收参数这一事实:

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

playground

注意我们如何能够将类型构造函数与其值分离。

这种模式非常灵活,可以创建库 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 |编号

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] };

playground

关于typescript - 您可以将类型别名传递给 TypeScript 中的接口(interface)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65730766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com