Reputation: 1145
For example, suppose I have:
export function trade(positions: object) {
...
}
but instead of just specifying that 'positions' is an object, I want to specify that it is an object with values that must be Strings.
What is the syntax for that?
Edit:
To tighten the example, suppose I just want keys and values to all be Strings. Is there a syntax for that?
Upvotes: 0
Views: 52
Reputation: 251262
If you want to share the object is as wide as "has string keys, and string values" you can use an index type:
export function trade(positions: {[key: string]: string;}) {
// ...
}
If you want to make it more specific, it may be time to create an interface - even if everything is optional - as it will give you more design-time help when creating a positions object.
interface Positions {
x?: string;
y?: string;
z?: string;
t?: string;
}
This doesn't force any of the items to be present, but you'll get good auto-completion. If the members are required, you can make them non-optional.
Upvotes: 2