Issue
The following code will always produce an error, but as a sketch, this is what I aim to represent:
interface A {
[ key : string ] : string,
"anotherKey" : MyInterface
}
I’ve checked a few books, TS documentation, tested in ts-node, but I am not that well versed and need some help.
How can I specify an object whose most keys are "key":"value"
but some specific aren’t?
Solution
Use a regular type and intersection:
type MySpecialType = {
specific: { type: number; };
extremely: { specific: { type: string; } };
} & {
[key: string]: string;
};
&
is read as and
So this is saying this is a really specific type and an index signature of strings to strings.
Answered By – kellys
Answer Checked By – Timothy Miller (BugsFixing Admin)