gpt4 book ai didi

typescript - 避免在 switch 中进行 typescript 转换

转载 作者:搜寻专家 更新时间:2023-10-30 21:34:46 26 4
gpt4 key购买 nike

考虑以下代码:

interface FooBarTypeMap {
FOO: FooInterface;
BAR: BarInterface;
}

type FooBarTypes = "FOO" | "BAR";

export interface FooBarAction<T extends FooBarTypes> {
type: T;
data: FooBarTypeMap[T];
}

const doSomthingBasedOnType = (action: FooBarAction<FooBarTypes>): void => {
switch (action.type) {
case "FOO":
FooAction((action as FooBarAction<"FOO">));
}
};

const FooAction = (action: FooBarAction<"FOO">): void => {
//do something with action.data
};

现在我想避免在 doSomthingBasedOnType 中看到的强制转换(作为 FooBarAction<"FOO"> 的操作),如果接口(interface)使它成为此开关内的唯一可能性,则作为定义。我可以在我的代码中更改某些内容以使其正常工作,还是这只是 TypeScript 中的一个错误?

最佳答案

您需要将 FooBarAction 转换为可区分的联合。目前您的 FooBarAction 版本不是很严格,而 type 必须是 "FOO"| 之一“BAR”data 必须是 FooBarTypeMap[FooBarTypes] = FooInterface | 之一BarInterface 两者没有关系。所以这可以被允许:

let o : FooBarAction2<FooBarTypes> = {
type: "BAR",
data: {} as FooInterface
}

被歧视的联合版本看起来像这样:

export type FooBarAction = {
type: "FOO";
data: FooInterface;
} | {
type: "BAR";
data: BarInterface;
}

const doSomthingBasedOnType = (action: FooBarAction): void => {
switch (action.type) {
case "FOO":
FooAction(action);
}
};

// We use extract to get a specific type from the union
const FooAction = (action: Extract<FooBarAction, { type: "FOO" }>): void => {
//do something with action.data
};

您还可以使用条件类型的分配行为从类型的联合创建联合:

interface FooInterface { foo: number}
interface BarInterface { bar: number}
interface FooBarTypeMap {
FOO: FooInterface;
BAR: BarInterface;
}

type FooBarTypes = "FOO" | "BAR";

export type FooBarAction<T extends FooBarTypes> = T extends any ? {
type: T;
data: FooBarTypeMap[T];
}: never;


const doSomthingBasedOnType = (action: FooBarAction<FooBarTypes>): void => {
switch (action.type) {
case "FOO":
FooAction(action);
}
};

const FooAction = (action: FooBarAction<"FOO">): void => {
//do something with action.data
};

关于typescript - 避免在 switch 中进行 typescript 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53392498/

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