0

I want a type like:

interface Err {
  error: Error,
  [key: string]: string,
}

const e: Err = {
  error: new Error(),
  a: 'a',
  b: 'b',
}

to define an Object with the key named error and has another key with a string type. But has some Type lint errors:

(property) Err.error: Error Property 'error' of type 'Error' is not assignable to 'string' index type 'string'.

So how to define a type that must have a key named error with Error type and has some other keys with string type?

This problem is error and other keys are all string type, and I only want custom key is string type not Union Type like string | Error, so it's not like with this.

Thanks for your time.

Chuck
  • 1
  • 1
  • 3
  • Does this answer your question? [How to combine known interface properties with a custom index signature?](https://stackoverflow.com/questions/47592546/how-to-combine-known-interface-properties-with-a-custom-index-signature) – Roberto Zvjerković Jan 25 '22 at 10:17
  • The reason is, the indexer definition `[key: string]: ...` includes all other properties, because that's just how javascript works in the end. So in order for your type definition to make sense, the value type of the indexer definition must be a union of ALL possible value types, including explicitly defined properties – Marian Theisen Jan 25 '22 at 10:23
  • @RobertoZvjerković Looks similar but not the same. The key types in the original question are not the same, but it's all string type on my question. – Chuck Jan 25 '22 at 10:39
  • @MarianTheisen Thans for reply. I only want the custom key to be of type string, so no union type is used. – Chuck Jan 25 '22 at 10:44
  • Since javascript does not care about string or number in object keys, `[key: string]: string,` is redundant. – ABOS Jan 25 '22 at 13:25
  • > it's all string type on my question No it's not. One of the types is `Error`, that's the whole point. – Marian Theisen Jan 25 '22 at 14:15

0 Answers0