gpt4 book ai didi

typescript :从联合类型中排除未定义

转载 作者:行者123 更新时间:2023-12-05 05:54:45 27 4
gpt4 key购买 nike

有一个自定义类型Options 我创建的(嵌套对象)。我想使用类型 DefaultProperties创建一个包含默认属性的对象。

type Options = {
id: number,
text: string | undefined,
properties: {
title: string,
subtitle: string | undefined,
}
}

type DefaultProperties = Pick<Options, "properties">;

当然默认属性不能是undefined .所以我想排除 undefined从我的工会类型。为此目的,应该一个像NonNullable<T>这样的内置函数。或 Exclude<T,UnionType> .

我尝试了三种不同的方法来删除 undefined但没有解决方案适用于我的类型。

type Options = {
id: number,
text: string | undefined,
properties: {
title: string,
subtitle: string | undefined,
}
}

type DefaultProperties = Pick<Options, "properties">;


// Try #1: does not work
type Type1 = NonNullable<DefaultProperties>;

// Try #2: does not work
type NotOptional<Type> = {
[Property in keyof Type]-?: Type[Property];
};
type Type2 = NotOptional<DefaultProperties>;

// Try #3: does not work
type Type3 = Exclude<Options, undefined>;

示例: TypeScript Playground

如何删除所有 undefined s 来 self 所有的类型联合类型?

编辑:全部undefined s 应该从我的类型中删除。例如:

  • Options['text'] : string (没有更多 undefined )
  • Options['properties']['subtitle'] : string (没有更多 undefined )

稍后我想为此使用类型(例如):

const options : Options = { ... };
const defaults: DefaultProperties= { ... }; // With no undefined

const node : HTMLElement = .... ;
node.innerHTML = options.properties.subtitle !== undefined ? options.properties.subtitle : defaults.properties.subtitle;

defaults.properties.subtitle类型为 string | undefined ,编译器会抛出错误,因为 innerHTML只允许 strings .为此,我想排除 undefined .

最佳答案

您非常接近您的 NotOptional 实用程序类型和 Exclude,问题是您将 DefaultProperties 定义为一个对象properties 属性,但您正试图从 DefaultProperties 中删除 undefined,而不是从其 properties 属性中删除。

在评论中您曾说过您希望 DefaultPropertiesOptions 上的 properties 具有相同的形状。这意味着它的基本形状(在我们尝试删除 undefined 之前)是 Options["properties"]。所以:

type NotOptional<Type> = {
[Property in keyof Type]: Exclude<Type[Property], undefined>;
};

type DefaultProperties = NotOptional<Options["properties"]>;

测试:

const defaults1: DefaultProperties = {
title: "foo",
subtitle: undefined, // <== Error as desired
};

const defaults2: DefaultProperties = {
title: "foo",
subtitle: "something", // <== No error
};

Playground link

您还可以考虑删除可选性(结合您尝试过的两项操作):

type NotOptional<Type> = {
[Property in keyof Type]-?: Exclude<Type[Property], undefined>;
};

type DefaultProperties = NotOptional<Options["properties"]>;

测试:

const defaults1: DefaultProperties = {
title: "foo",
subtitle: undefined, // <== Error as desired (wrong type for `subtitle`)
};

const defaults2: DefaultProperties = { // <== Error as desired (missing `somethingElse`)
title: "foo",
subtitle: "something",
};

const defaults3: DefaultProperties = { // <== No error
title: "foo",
subtitle: "something", // <== No error
somethingElse: "x",
};

Playground link

关于 typescript :从联合类型中排除未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69581746/

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