Skip to content

Suggestion: Support type assertion on destructuring syntax #18229

Closed
@zheeeng

Description

@zheeeng

Code

Maybe we need a convenient way to add assertions on variables which are destructured out.

    type StringOrNumber = string | number
    
    const obj = {
      foo: 42 as StringOrNumber
    }
    
    const { foo } = obj

Nowadays, the workarounds are:

    // A:
    const { foo } = obj as { foo: number }

    // B:
    const { foo: foo2 } = obj

    const foo = <number>foo2

The first is a burden to rewrite the obj's type when its type is nested and complicated. The second seems weird.

I'm assuming such a syntax like:

    const { <number>foo } = obj

can absolutely help us asserting the type no matter how big the obj's type is.

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