0

I am using TypeScript and i am wondering how can i "type" the following object.

{
    "code": 52,
    "somerandomstring123":"Some Data"
}

The thing is, that the KEY NAME somerandomstring123 is dynamically generated string and is not constant. So for example, the JSON could've been like this:

{
    "code": 52,
    "someOTHERstring456":"Some Data"
}

I tried using [key:string]: string like this:

interface SomeObjectData {
code: number;
[target: string]: string;
}

but it returns an error of: Property 'code' of type 'number' is not assignable to 'string' index type 'string'.

P.S.: The dynamic named property (e.g. somerandomstring123) value type is a string, so using string | number is not something i need.

John1256
  • 13
  • 4
  • if you know it will either be a string or a number you can simply use a union. `[target: string]: string | number` – about14sheep Jan 03 '22 at 00:56
  • @about14sheep uh oops i fixed my post, i am not talking about the value type but the key type. – John1256 Jan 03 '22 at 01:10
  • I think @about14sheep is correct. That should make TS happy. The problem is that this "dynamic" expression: `[target: string]: string` will also encompass the `code` property, so it needs to be a union. – tromgy Jan 03 '22 at 01:15
  • There is no specific object type in TypeScript corresponding to the set of values you wish to support. You can make a generic constraint like [this](https://tsplay.dev/N9pk7w) which requires an object has a numeric `code` property and that other properties must be of `string` type, but using it has lots of caveats. I'm inclined to close this as a duplicate of [this question](https://stackoverflow.com/q/61431397/2887218); yours asks about "one" dynamic key, but the issue is nearly the same (indeed your attempt at using `[key: string]: string` shows that). Let me know if I'm missing something. – jcalz Jan 03 '22 at 02:07
  • Try `type SomeObjectData = Record & { code: number, /* other non-string properties */ };` – Parzh from Ukraine Jan 03 '22 at 10:57
  • Does this answer your question? [How do I dynamically assign properties to an object in TypeScript?](https://stackoverflow.com/questions/12710905/how-do-i-dynamically-assign-properties-to-an-object-in-typescript) – Parzh from Ukraine Jan 03 '22 at 11:21

2 Answers2

0

you can use the union type for the target.

interface SomeObjectData {
    [target: string]: string | number;
    code: number;
}

const data:SomeObjectData = { code: 12}
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
  • Does it make sense to allow _all other properties_ to be `number` just to be able to add `code: number`? If the OP knows that the dynamic properties are strings, then `string | number` is excess; if the OP doesn't know the type of dynamic properties, the type should rather be `unknown` – Parzh from Ukraine Jan 03 '22 at 11:18
  • @Dima Parzhitsky is right. I know that the dynamically named property will always be a string. – John1256 Jan 03 '22 at 12:56
0

Try this:

type SomeObjectData = {
  [key: string]: string; // dynamic properties
} & {
  code: number; // static properties
};
Parzh from Ukraine
  • 7,999
  • 3
  • 34
  • 65