Skip to content

Function inference is incorrect with inline const objectsΒ #59721

@Binek115

Description

@Binek115

πŸ”Ž Search Terms

inline inference, function return type, as const

πŸ•— Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAZmAjDAvDAFASlQPhgWQEMoALAOgCdCwATEAWyxjwAYYB+DAbxkIC4YAckSCYAX14QYoSFGwCeAIwGDFosQCgNM6PDAAmVBmwo8XDTGnhdIRQCsApsCgBBCAGFrsNOh78hIuKSVrKYFjAUDlAArhRgBMTkVLQMTKwcMLaOzm6esjAKMMpCauIA3BpiQA

πŸ’» Code

const fn1 = () => Math.random() > 0 ? ({ a: '1' } as const) : { b: 'b' }

const fn2 = () => {
  const objectAsConst = ({ a: '1' } as const)
  return Math.random() > 0 ? objectAsConst : { b: 'b' };
}

πŸ™ Actual behavior

Return type for fn1 is

{
    readonly a: "1";
    b?: undefined;
} | {
    b: string;
    readonly a?: undefined;
}

while for fn2 is

{
    readonly a: "1";
} | {
    b: string;
}

πŸ™‚ Expected behavior

Return type for both functions should be

{
    readonly a: "1";
} | {
    b: string;
}

Inlining object should not change type of function

Additional information about the issue

No response

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