#Inconsistent typings for arrays with nullish values

1 messages · Page 1 of 1 (latest)

scarlet summit
#

I just found out that this code:

const arr = [1, 2, 3, null]; // number[] or (number | null)[] ?
const arr = [1, 2, 3, undefined]; // number[] or (number | undefined)[] ?

gives different typings results in different projects, and i'm not sure why this would change.

However this seems okay:

const arr = [1, 2, 3, '4']; // (number | string)[]

In Stackblitz, number[]: https://stackblitz.com/edit/typescript-mxv8v6?file=index.ts
In TS Playground, (number | null)[]: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAhgJwQOQK4Bt0wLwwNoCMANDAEwkDMJYG6AugNwBQoksiCAqmACYCmAMwCWYPjxz5iZSiVS9BIsYxbho8JAGUoCEQHMJhEuRhUYAcgAsZ5UA

This difference is also present in some of my projects, and i don't know what config would influence this.

Does anyone know ?

obtuse lilyBOT
#

@scarlet summit Here's a shortened URL of your playground link! You can remove the full link from your message.

Ourson#7202

Preview:ts const arrNull = [1, 2, 3, null] const arrUndefined = [1, 2, 3, undefined] const arrString = [1, 2, 3, "4"]

soft raptor
#

some may have exactOptionalPropertyTypes disabled

scarlet summit
#

Inconsistent typings for arrays with nullish values

soft raptor
#

since disabled is the default state

scarlet summit
#

hmm exactOptionalPropertyTypes does change this behaviour.
However, i just found out strictNullChecks does, which seems obvious, now that i see it

#

!resolved

soft raptor
#

ah woops, of course