带有keyof的打字稿预设键

我想预设一个键,但是我不想限制用户仅使用这些键。

type B = {
  a: string;
  b: number;
}

type T = keyof B | string;


function someFunc(key: T) {}

someFunc(); // key type is `T`

In the above case, I want to get autocomplete for a and b, but users can also use any string they want. Is it possible?

评论
红颜祸水
红颜祸水
type B = {
  a: string;
  b: number;
}

type C = {[key:string]: string | number } & B

const c: C = {
  a: "123",
  b: 123,
  d: "123"
}

With working auto completion. Check out here

点赞
评论