英文:
How to make all keys of a type required while keeping undefined as possible value?
问题
我想将类型的键/值映射如下:
required_key: string | undefined
到required_key: string | undefined
(相同)required_key: string
到required_key: string
(相同)optional_key?: string | undefined
到optional_key: string | undefined
(去除键中的 ?)optional_key?: string
到optional_key: string | undefined
(去除键中的 ? 并添加 undefined 作为可能的值)
interface Person {
firstName: string | undefined;
middleName?: string;
lastName?: string | undefined;
}
// 我需要的是:
// interface Person {
// firstName: string | undefined;
// middleName: string | undefined;
// lastName: string | undefined;
// }
type Normalize<T> = { [K in keyof T]-?: T[K] };
// 使用 Normalize<Person> 后得到的结果:
// interface Person {
// firstName: string | undefined;
// middleName: string;
// lastName: string;
// }
const test: Normalize<Person> = {
firstName: undefined,
middleName: undefined, // ts 错误,类型为 string,不允许 undefined
lastName: undefined, // ts 错误,类型为 string,不允许 undefined
};
在 TypeScript 中是否可能实现这个要求?似乎 -?
移除了键和值中的 undefined,但我不知道如何在键是可选的情况下添加 undefined 值选项。
我阅读了这个问题/回答:https://stackoverflow.com/questions/72402413/is-it-possible-to-make-a-property-required-yet-preserve-undefined,我了解到 exactOptionalPropertyTypes
编译器标志,但它仍然没有解决如何将 key?: string
转换为 key: string | undefined
。
英文:
I want to map key/values of a type as follows:
required_key: string | undefined
torequired_key: string | undefined
(same)required_key: string
torequired_key: string
(same)optional_key?: string | undefined
tooptional_key: string | undefined
(removed ? from key)optional_key?: string
tooptional_key: string | undefined
(removed ? from key and added undefined as possible value)
interface Person {
firstName: string | undefined;
middleName?: string;
lastName?: string | undefined;
}
// What I need:
// interface Person {
// firstName: string | undefined;
// middleName: string | undefined;
// lastName: string | undefined;
// }
type Normalize<T> = { [K in keyof T]-?: T[K] };
// What I get with Normalize<Person>:
// interface Person {
// firstName: string | undefined;
// middleName: string;
// lastName: string;
// }
const test: Normalize<Person> = {
firstName: undefined,
middleName: undefined, // ts err, type = string, undefined not allowed
lastName: undefined, // ts err, type = string, undefined not allowed
};
Is it possible in typescript? It seems that -?
is removing undefined from both key and value and I don't know how to add undefined value option if key is optional.
I read this Q/A https://stackoverflow.com/questions/72402413/is-it-possible-to-make-a-property-required-yet-preserve-undefined and I know about exactOptionalPropertyTypes
compiler flag but it still does not solve how to convert key?: string
to key: string | undefined
.
答案1
得分: 1
以下是翻译好的部分:
type AllOptionalKeys<T> = { [K in keyof T]-?: undefined extends T[K] ? K : never; }[keyof T];
type AllNonOptionalKeys<T> = { [K in keyof T]-?: undefined extends T[K] ? never : K; }[keyof T];
type Normalize<T> = { [K in AllOptionalKeys<T>]: T[K] | undefined } & { [K in AllNonOptionalKeys<T>]: T[K]; };
英文:
Not sure if this is the best solution, but you can achieve it with the following:
type AllOptionalKeys<T> = { [K in keyof T]-?: undefined extends T[K] ? K : never; }[keyof T];
type AllNonOptionalKeys<T> = { [K in keyof T]-?: undefined extends T[K] ? never : K; }[keyof T];
type Normalize<T> = { [K in AllOptionalKeys<T>]: T[K] | undefined } & { [K in AllNonOptionalKeys<T>]: T[K]; };
The two intermediate types allow to strip the ?
from the keys, but keep track of which are optional.
Then it constructs an union type, where all optional keys have a value of T[K] | undefined
, and all the non optional keys have T[K]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论