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?
Add a ? to your parameters inside the interface for the optional ones. Like option1?: string etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With