dynamically adding a property name to a typescript interface
typescript
Solution
You have to specify the type of name. There's no way to use it in an object declaration but you can use the [ ] to set and access the property value.
interface MyInterface {
[name: string]: string;
}
const n = 'qweq';
let x: MyInterface = {
'a': 'b'
}
x[n] = 'a';
And access it this way.
x[n]
Check it out in the playground here.
Problem
I have a constant: ``` const name = 'some/property'; ``` I'd like to define an interface that uses name as a key for a property in a similar way to using it in an object declaration like so: ``` {[name]: 'Bob'} ``` I tried the following, but it seems that this is doing something else: ``` interface MyInterface { [name]: string; } ``` is dynamically defining property names supported in typescript?