Is it possible to have mixed specific typed keys in a TypeScript type, and a generic key type?

empyrical

I'm trying to create a type to describe an ES6 Proxy object where I will know the types for a few keys, and the rest of the keys will be generic with a callback as a value and I won't know their names until runtime.

However, if I try something like this:

interface MyCallback {
  (): void;
}

interface MyType {
    myKey1: number;
    [key: string]: MyCallBack;
}

I get errors like:

[ts] Property 'myKey1' of type 'number' is not assignable to string index type 'MyCallback'.

If I add [key: string]: number, I get the error Duplicate string index signature.

If I overload it so it's like number | MyCallback, I get this error if I try to call a callback on a MyType instance:

[ts] Cannot invoke an expression whose type lacks a call signature. Type 'number | MyCallback' has no compatible call signatures.

Is it possible to have a type like I'm trying to create in TypeScript?

bryan60

the answer is sort of. You can accomplish this with intersection types:

interface MyType {
    myKey1: number;
}

interface MyCallBack {
    (): void;
}

interface GenericCallbackType {
    [key: string]: MyCallBack;
}

type CombinedType = MyType & GenericCallbackType;

const obj: CombinedType = {
    myKey1: 8,
    anyString: () => {}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Generate a type in Typescript that will have as many possible shapes that there are keys on another type

Enforce Typescript generic type to have keys that start with x

Is it possible to detect type of generic in TypeScript?

Are generic type aliases possible in TypeScript?

Loosely typed object with union type key in typescript

TypeScript object type with keys from other object type where the value of the key matches a specific type

In Typescript how do I allow a type to have all keys of a generic that point to a recursive value of that type?

Typescript: Type unwrapping properties of specific generic type

Have a Generic Type of a Method Depend on the Generic Type Of the Class in TypeScript

How to use Typescript type object indexing for typed generic functions?

Typescript new type from object keys already typed as an interface

Typescript: Spreading Objects into a typed Object contains keys not part of the type

TypeScript: mixed behaviour for function with multiple parameters of same generic type

Is it possible to have an optional generic type for a trait?

Is it possible to reuse overloading type with generic in typescript

TypeScript: Is it possible to get the return type of a generic function?

Is it possible to make generic mapped object type in TypeScript?

Typescript: Is it possible to evaluate generic type into string literal?

Is it possible to extract Map keys as type in TypeScript?

Typescript create key using a generic type

Mapped Object Type with Generic Per Key in TypeScript

How to use typescript generic type as an object key

Partial keys of union type as key of an object in typescript

TypeScript type with specific set of keys and values

Typescript object with keys of specific type (and working intellisense)

TypeScript Generic type of a generic type

How to enforce narrowing to specific type of a typed const in typescript?

How can I have TypeScript guarantee that a generic type has a property that implements a specific method?

How to declare a generic function in TypeScript to have only one property of an object T to be of a specific type?