在lodash中,_.invert函数反转对象的键和值:
var object = { 'a': 'x', 'b': 'y', 'c': 'z' };
_.invert(object);
// => { 'x': 'a', 'y': 'b', 'z': 'c' }lodash类型currently声明总是返回一个stringstring→映射:
_.invert(object); // type is _.Dictionary<string>但有时,尤其是在使用const assertion的情况下,更精确的类型会更合适:
const o = {
a: 'x',
b: 'y',
} as const; // type is { readonly a: "x"; readonly b: "y"; }
_.invert(o); // type is _.Dictionary<string>
// but would ideally be { readonly x: "a", readonly y: "b" }有可能得到如此精确的类型吗?下面的声明很接近:
declare function invert<
K extends string | number | symbol,
V extends string | number | symbol,
>(obj: Record<K, V>): {[k in V]: K};
invert(o); // type is { x: "a" | "b"; y: "a" | "b"; }键是正确的,但是值是输入键的并集,即您失去了映射的特异性。有可能让这一切变得完美吗?
发布于 2019-06-02 22:59:44
您可以使用更复杂的映射类型来保留正确的值:
const o = {
a: 'x',
b: 'y',
} as const;
type AllValues<T extends Record<PropertyKey, PropertyKey>> = {
[P in keyof T]: { key: P, value: T[P] }
}[keyof T]
type InvertResult<T extends Record<PropertyKey, PropertyKey>> = {
[P in AllValues<T>['value']]: Extract<AllValues<T>, { value: P }>['key']
}
declare function invert<
T extends Record<PropertyKey, PropertyKey>
>(obj: T): InvertResult<T>;
let s = invert(o); // type is { x: "a"; y: "b"; }AllValues首先创建一个包含所有key、value对的联合(因此对于您的示例,它将是{ key: "a"; value: "x"; } | { key: "b"; value: "y"; })。在映射的类型中,我们将映射到联合中的所有value类型,并且对于每个value,我们使用Extract提取原始key。只要没有重复的值,这就可以很好地工作(如果有重复的值,我们将在值出现的位置获得键的并集)
发布于 2019-08-30 20:32:54
Titian Cernicova-Dragomir的解决方案真的很酷。今天,我找到了另一个用条件类型交换对象键和值的方法:
type KeyFromValue<V, T extends Record<PropertyKey, PropertyKey>> = {
[K in keyof T]: V extends T[K] ? K : never
}[keyof T];
type Invert<T extends Record<PropertyKey, PropertyKey>> = {
[V in T[keyof T]]: KeyFromValue<V, T>
};使用const o进行测试
const o = {
a: "x",
b: "y"
} as const;
// type Invert_o = {x: "a"; y: "b";}
type Invert_o = Invert<typeof o>;
// works
const t: Invert<typeof o> = { x: "a", y: "b" };
// Error: Type '"a1"' is not assignable to type '"a"'.
const t1: Invert<typeof o> = { x: "a1", y: "b" };使用返回类型Invert<T>,以与上面相同的方式声明invert函数。
发布于 2020-09-30 05:11:54
有了TypeScript 4.1对Key Remapping in Mapped Types的支持,这变得相当简单
const o = {
a: 'x',
b: 'y',
} as const;
declare function invert<
T extends Record<PropertyKey, PropertyKey>
>(obj: T): {
[K in keyof T as T[K]]: K
};
let s = invert(o); // type is { readonly x: "a"; readonly y: "b"; }https://stackoverflow.com/questions/56415826
复制相似问题