Destructuring multiple type using default values

Viewed 88

I want to use destructuring on multiple type with common properties and default value for others.

interface Software {
    id: number;
    name: string;
}

interface VersionedSoftware extends SoftwareShape {
    version: string;
}

I tried both following solutions.

function post1({ id, version = '' }: Software | VersionedSoftware) {
    // ...
}

function post2({ id, ...versioned }: Software | VersionedSoftware) {
    const { version = '' } = versioned;
    // ...
}

But got error

Property 'version' does not exist on type 'Software | VersionedSoftware'.

I would expect it to work as I defined a default value for version property

Is there any syntax allowing this usage ?

0 Answers
Related