When looking at the code snippet below, I was under the impression that the destructured array variables, firstName
and lastName
, would be classified as string | undefined
. This is because the array being destructured could have fewer variables compared to what is declared, causing any additional declared variables to default to undefined
. However, in TypeScript, these variables are recognized only as type string
. Can anyone shed some light on why this discrepancy exists? Many thanks.
const [firstName, lastName] = fullName.split(' ')
// Typescript produces these types:
// const firstName: string
// const lastName: string