是否可以在 TypeScript 类型和通用键类型中混合使用特定类型的键?
Is it possible to have mixed specific typed keys in a TypeScript type, and a generic key type?
我正在尝试创建一个类型来描述一个 ES6 代理对象,我将在其中知道一些键的类型,其余键将是通用的,带有回调作为值,但我不会直到运行时才知道他们的名字。
但是,如果我尝试这样的事情:
interface MyCallback {
(): void;
}
interface MyType {
myKey1: number;
[key: string]: MyCallBack;
}
我收到如下错误:
[ts] Property 'myKey1' of type 'number' is not assignable to string index type 'MyCallback'.
如果我添加 [key: string]: number
,我得到错误 Duplicate string index signature
。
如果我重载它,就像 number | MyCallback
,如果我尝试在 MyType
实例上调用回调,我会收到此错误:
[ts] Cannot invoke an expression whose type lacks a call signature. Type 'number | MyCallback' has no compatible call signatures.
是否可以像我尝试在 TypeScript 中创建的类型一样?
答案有点像。您可以使用交集类型来完成此操作:
interface MyType {
myKey1: number;
}
interface MyCallBack {
(): void;
}
interface GenericCallbackType {
[key: string]: MyCallBack;
}
type CombinedType = MyType & GenericCallbackType;
const obj: CombinedType = {
myKey1: 8,
anyString: () => {}
}
如评论中所述,接受的答案不适用于作业,导致 Property 'myKey1' is incompatible with index signature
错误。要处理作业,我们可以使用 @jcalz's answer :
interface MyCallback {
(): void
}
interface MyType {
myKey1: number
}
const asCombinedType = <C>(
res: C & MyType & Record<Exclude<keyof C, keyof MyType>, MyCallback>
): C => res
const obj = asCombinedType({
anyKey: () => { /* ...*/ },
myKey1: 12
})
诚然有点复杂,但它完成了工作。
已接受的答案对我不起作用,此代码段有效:Playground Link
interface MyType {
myKey1: number;
}
interface GenericCallbackType {
[key: string]: () => void;
}
type CombinedType = MyType | GenericCallbackType;
const obj: CombinedType = {
myKey1: 8,
anyString: () => {}
}
我正在尝试创建一个类型来描述一个 ES6 代理对象,我将在其中知道一些键的类型,其余键将是通用的,带有回调作为值,但我不会直到运行时才知道他们的名字。
但是,如果我尝试这样的事情:
interface MyCallback {
(): void;
}
interface MyType {
myKey1: number;
[key: string]: MyCallBack;
}
我收到如下错误:
[ts] Property 'myKey1' of type 'number' is not assignable to string index type 'MyCallback'.
如果我添加 [key: string]: number
,我得到错误 Duplicate string index signature
。
如果我重载它,就像 number | MyCallback
,如果我尝试在 MyType
实例上调用回调,我会收到此错误:
[ts] Cannot invoke an expression whose type lacks a call signature. Type 'number | MyCallback' has no compatible call signatures.
是否可以像我尝试在 TypeScript 中创建的类型一样?
答案有点像。您可以使用交集类型来完成此操作:
interface MyType {
myKey1: number;
}
interface MyCallBack {
(): void;
}
interface GenericCallbackType {
[key: string]: MyCallBack;
}
type CombinedType = MyType & GenericCallbackType;
const obj: CombinedType = {
myKey1: 8,
anyString: () => {}
}
如评论中所述,接受的答案不适用于作业,导致 Property 'myKey1' is incompatible with index signature
错误。要处理作业,我们可以使用 @jcalz's answer
interface MyCallback {
(): void
}
interface MyType {
myKey1: number
}
const asCombinedType = <C>(
res: C & MyType & Record<Exclude<keyof C, keyof MyType>, MyCallback>
): C => res
const obj = asCombinedType({
anyKey: () => { /* ...*/ },
myKey1: 12
})
诚然有点复杂,但它完成了工作。
已接受的答案对我不起作用,此代码段有效:Playground Link
interface MyType {
myKey1: number;
}
interface GenericCallbackType {
[key: string]: () => void;
}
type CombinedType = MyType | GenericCallbackType;
const obj: CombinedType = {
myKey1: 8,
anyString: () => {}
}