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
@DanielRosenwasser

Description

@DanielRosenwasser

Found in Formik by @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.'
    }
}

Metadata

Metadata

Assignees

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