Skip to content

Indexed access and object destructuring on never doesn't type errorย #56778

@TkDodo

Description

@TkDodo

๐Ÿ”Ž Search Terms

"never", "indexed access", "destruct", "property access"

๐Ÿ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries

โฏ Playground Link

https://www.typescriptlang.org/play?&code/MYewdgzgLgBApgDwIYFsAOAbOAuGY4BucATjALx4CuGGMSEehJAUMwPRswACUEAtIjRxgUAcWIhSABQlDiUAJ4wA5ADMQIZTAAmIOAzAhYiAJbQY4GIqEr8RYsoB0ACgBMAZncBOAJTNEqJhwjuogrBzwCEIiJmAA5lYAFnAwEKgpJBKkycRwADQwAEaUsFA5KWZ44HDsnDz8gsKimZIwMiByiiqhWrr6VcYIZrCW1inKdiRObp6%20-sjoWADaahrKALpAA=#code/MYewdgzgLgBApgDwIYFsAOAbOAuGY4BucATjALx4CuGGMSEehJAUMwPRswACUEAtIjRxgUAcWIhSABQlDiUAJ4wA5ADMQIZTAAmIOAzAhYiAJbQY4GIqEr8RYsoB0ACgBMAZncBOAJTNEqJhwjuogrBzwCEIiJmAA5lYAFnAwEKgpJBKkycRwADQwAEaUsFA5KWZ44HDsnDz8gsKimZIwMiByiiqhWrr6VcYIZrCW1inKdiRObp6+-sjoWADaahrKALrhnACSytp0GBAgkdGlyanp8OKt5QXFZyQVBtW13LwCUU1iWW2yJF2rTQ6PTPQbDCxgKwKGwTJgOFwebx+UCQWAAbxgoRgAF9yJFAlggA

๐Ÿ’ป Code

// Given an `example` variable of type `never`:

const example: never = null as never

// this errors correctly
// @ts-expect-error Property 'foo' does not exist on type 'never'.(2339)
example.foo

// expecting the same error here, but there is none
// @ts-expect-error Property 'foo' does not exist on type 'never'.(2339)
example['foo']

// I'd also expect the same error here, but there is none
// @ts-expect-error Property 'foo' does not exist on type 'never'.(2339)
const { foo } = example

๐Ÿ™ Actual behavior

There is an error on the property access (OK), but no error on the indexed access (NOK) and also no error when destructing (NOK)

๐Ÿ™‚ Expected behavior

I'd expect the same error in all three cases

Additional information about the issue

Had a discussion with @Andarist about this on twitter:

https://x.com/AndaristRake/status/1735078956917723358

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions