Skip to content

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

Closed as not planned
@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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions