Skip to content

Missing property when inferring from object literals in array literalsΒ #53914

@dragomirtitian

Description

@dragomirtitian

Bug Report

πŸ”Ž Search Terms

object literal method union reduce

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

let a =  [
    { foo: 0, m() { } },
    { bar: 1 }
]
declare let n: number
a[n].m

πŸ™ Actual behavior

TypeScript behaves as if m in present on a[n] when trying to access in a[n].m (as it does for foo and bar as well), but generated declarations don't reflect this:

declare let a: ({
    foo: number;
    m(): void;
    bar?: undefined;
} | {
    bar: number;
    foo?: undefined;
})[];

With the above declaration for a (from generated declarations), we couldn't access a[n].m (PL).

Tooltips also display the same type as declarations omitting m in the second constituent of the union.

πŸ™‚ Expected behavior

Methods in object literals should behave the same as properties when the type of the array is inferred (if a method is missing in one of the objects, an optional member with type undefined should be inferred:

declare let a: ({
    foo: number;
    m(): void;
    bar?: undefined;
} | {
    bar: number;
    foo?: undefined;
    m?: undefined;
})[];

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions