Skip to content

Use generic defaults for "empty" constructors of Array, Map, Set #29786

Closed
@aleclarson

Description

@aleclarson

TypeScript Version: 3.4.0-dev.20190202

Search Terms:

  • assignment expression
  • RHS infer

Code

class Test {
    foo?: Map<object, string>

    getFoo(obj: object) {
        // "foo" is inferred as "Map<any, any>" instead of "Map<object, string>"
        const foo = this.foo || (this.foo = new Map())
        foo.set(obj, 1) // No error!
    }
}

Expected behavior: Infer foo to be Map<object, string>

Actual behavior: Infers foo to be Map<any, any>

Playground Link: Click here

Related Issues: Maybe #7782

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions