TS18004

error TS18004: No value exists in scope for the shorthand property ‘age‘. Either declare one or provide an initializer.

Broken Code ❌

1
2
3
4
5
6
7
export function getPerson() {
return {
age,
fistName: 'Benny',
lastName: 'Neugebauer',
};
}

Fixed Code ✔️

If you want to use the shorthand property name syntax to access the age property, you have to make sure that this variable is defined in the first place:

1
2
3
4
5
6
7
8
export function getPerson() {
const age = 34;
return {
age,
fistName: 'Benny',
lastName: 'Neugebauer',
};
}

Alternatively, you can avoid using the shorthand property name syntax:

1
2
3
4
5
6
7
export function getPerson() {
return {
age: 34,
fistName: 'Benny',
lastName: 'Neugebauer',
};
}