Reputation: 1988
I have this interface:
interface Metadata {
key: string,
value: string,
option1: string, // optional parameter
option2: string, // optional parameter
option3: string // optional parameter
}
When i try to create a typed object in this way:
const a: Metadata = {key: 'obligatory', value: 'obligatory', option1 : 'optional '};
i got this error:
error TS2322: Type '{key: 'obligatory', value: 'obligatory', option1 : 'optional '}' is not assignable to type 'Metadata'.
Property 'option2' is missing in type '{key: 'obligatory', value: 'obligatory', option1 : 'optional '}'.
Is it possible to create an object without having to pass all the properties?
Upvotes: 3
Views: 904
Reputation: 896
Add a ? to your parameters inside the interface for the optional ones. Like option1?: string
etc.
Upvotes: 5