有没有办法将联合类型转换为交集类型:
type FunctionUnion = () => void | (p: string) => void
type FunctionIntersection = () => void & (p: string) => void
Run Code Online (Sandbox Code Playgroud)
我想应用转换FunctionUnion来获取FunctionIntersection
说我有清单
const list = ['a', 'b', 'c']
是不是可以从这个值的联合类型派生出来的'a' | 'b' | 'c'?
我想要这个,因为我想定义只允许来自静态数组的值的类型.而且我还需要在运行时枚举这些值,所以我使用数组.
示例如何使用索引对象实现:
const list = ['a', 'b', 'c']
'a' | 'b' | 'c'
const list = ['a', 'b', 'c']
但我想知道没有这个索引地图是否可行.
我有一个字符串联合类型,如下所示:
type Suit = 'hearts' | 'diamonds' | 'spades' | 'clubs';
Run Code Online (Sandbox Code Playgroud)
我想要一种类型安全的方法来获取可以在此字符串联合中使用的所有可能值.但由于接口主要是设计时构造,我能做的最好的就是:
export const ALL_SUITS = getAllStringUnionValues<Suit>({
hearts: 0,
diamonds: 0,
spades: 0,
clubs: 0
});
export function getAllStringUnionValues<TStringUnion extends string>(valuesAsKeys: { [K in TStringUnion]: 0 }): TStringUnion[] {
const result = Object.getOwnPropertyNames(valuesAsKeys);
return result as any;
}
Run Code Online (Sandbox Code Playgroud)
这没关系,该函数确保我总是传递一个对象,其中每个键是字符串union中的一个元素,并且每个元素都包含在内,并返回所有元素的字符串数组.因此,如果字符串union更改,则在编译时对此函数的调用将发生错误,如果还未更新的话.
但问题是常量的类型签名ALL_SUITS是('hearts' | 'diamonds' | 'spades' | 'clubs')[].换句话说,TypeScript认为它是一个包含没有或多个这些值的数组,可能带有重复项,而不是只包含所有值的数组,例如['hearts', 'diamonds', 'spades', 'clubs'].
我真正喜欢的是我的泛型getAllStringUnionValues函数指定它返回的方法['hearts', 'diamonds', 'spades', 'clubs'].
我怎样才能做到这一点一般,同时为干越好?
我有这个:
interface Obj {
foo: string,
bar: number,
baz: boolean
}
Run Code Online (Sandbox Code Playgroud)
所需的类型是这个元组:
[string, number, boolean]
Run Code Online (Sandbox Code Playgroud)
如何将接口转换为元组?
更新:
我最初的问题是:我用声明式的精神制作了一些自以为是的库,用户应该在对象文字中描述函数的参数。像这样:
let paramsDeclaration = {
param1: {
value: REQUIRED<string>(),
shape: (v) => typeof v === 'string' && v.length < 10
},
param2: {
value: OPTIONAL<number>(),
...
},
}
Run Code Online (Sandbox Code Playgroud)
然后库接受这个对象并创建一个带有参数的函数:
(param1: string, param2?: number) => ...
Run Code Online (Sandbox Code Playgroud)
所以,制作这样的功能不是问题,问题是正确键入它,以便用户获得良好的代码完成(IntelliSense)。
PS我知道它无法解决,但是知道什么是最接近的解决方法/hack会很有趣。
我正在尝试创建一个映射元组,该元组可以深入了解成员的类型:
export type Argument<T = unknown> = {
value: T;
};
export type TypesOf<T extends Argument[]> = {[Index in keyof T]: T[Index]["value"]};
Run Code Online (Sandbox Code Playgroud)
所以,TypesOf<[{value: number}, {value: string}]>应该产生[number, string].
但是,我收到此错误:
Type '"value"' cannot be used to index type 'T[Index]'.
Run Code Online (Sandbox Code Playgroud)
编辑:
应用@jcalz的解决方案后的额外问题:
const tuple = [{value: 1}, {value:"foo"}] as const;
type V = TypesOf<typeof tuple>;
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Type 'readonly [{ readonly value: 1; }, { readonly value: "foo"; }]' does not satisfy the constraint 'Argument<unknown>[]'.
The type 'readonly [{ readonly …Run Code Online (Sandbox Code Playgroud) type stringUnion = "Value1"|"Value2"
我想从所有字符串中获取并创建一个数组stringUnion
所以数组看起来像这样["Value1","Value2"],我想要一个数组,这样我就可以像这样迭代它:
theArray.map((current, index)=>{
})
Run Code Online (Sandbox Code Playgroud)
我怎么做?
我想知道是否可以在打字稿中键入对象中动态属性的最大数量。
所以基本的例子是跟踪事件:
events.track('SOME_EVENT', { first: 'a', other: 'b', some: 'c'})
Run Code Online (Sandbox Code Playgroud)
事件数据应该最多保存 3 个属性及其各自的值,键也可以是动态的。
我用 basic 键入它Record,但允许的属性数量没有限制:
export interface Events {
track: (name: string, params?: Record<string, string | number | unknown>) => void;
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?