8

I have the following code of a type User that has optional type Address that I want to desctructure in a single line. However, when I try, I am getting an error saying:

Property 'street' does not exist on type 'Address | undefined'

Click here to see the Typescript playground of the code provided bellow

type User = {
 age: number;
 address?: Address;
}

type Address = {
 street?: string;
}

const user: User = {
 age: 22,
 address: {}
}

const {age, address: {street}} = user

Here the street does not exist on type Address | undefined

1 Answer 1

16

You need to initialize the address in your const.

Like this:

const {age, address : {street} = {}} = user;

PlaygroundLink

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.