Skip to content

Weird uninitialized variable error for destructured members of type parameter constrained to homomorphic mapped type where member is declared optional #20994

Closed

Description

Found in Formik by Wesley Wigham (@weswigham) who patiently worked with me to find the minimal repro.

Run the following with strictNullChecks

interface Options {
    a?: number | object;
    b: () => void;
}

class C<T extends Options> {
    foo!: { [P in keyof T]: T[P] }

    method() {
        let { a, b } = this.foo;
        !(a && b)
        a
        // TypeScript issues an error on 'a' above:
        // Variable 'a' is used before being assigned.'
    }
}

Activity

  1. rozzzly commented on Jan 4, 2018

    @rozzzly

    Daniel Rosenwasser (@DanielRosenwasser)

        foo!: { [P in keyof T]: T[P] }

    Is the !: some new/obscure syntax I'm not aware of? Or just a typo?

  2. ahejlsberg commented on Jan 4, 2018

    @ahejlsberg
    Member
  3. sandersn commented on Jan 19, 2018

    @sandersn
    Member

    Should be fixed #20995

  4. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions