Skip to content

Type parameter can leak out of a function instead of being bound to specific type#43961

@jsnajdr

Description

@jsnajdr

Bug Report

🔎 Search Terms

type parameter leaks out

🕗 Version & Regression Information

In the TS playground, the first version that shows the bug is 3.5.1. Version 3.3.3 also behaves incorrectly, but in a different way.

⏯ Playground Link

Playground link with relevant code

💻 Code

functionwithP1<P>(p: P){constm=<I>(from: I)=>({ ...from, ...p});returnm;}constaddP1=withP1({foo: 1});constadded1=addP1({bar: 2});console.log(added1.foo,added1.bar);constcreateTransform=<I,O>(tr: (from: I)=>O)=>tr;functionwithP2<P>(p: P){constm=<I>(from: I)=>({ ...from, ...p});returncreateTransform(m);}constaddP2=withP2({foo: 1});constadded2=addP2({bar: 2});console.log(added2.foo,added2.bar);

🙁 Actual behavior

The added2.foo expression reports a type error:

Property 'foo' does not exist on type '{bar: number} & P'. 

The index.d.ts generated from this source has a declaration for addP2 that contains an undefined identifier P:

declareconstaddP2: <I>(from: I)=>I&P;

On the other hand, the addP1 version works well:

declareconstaddP1: <I>(from: I)=>I&{foo: number};

The only difference is that withP2 calls an additional createTransformer function which is an identity.

🙂 Expected behavior

Both P1 and P2 versions behave the same way and infer the return type correctly, without leaking the P type parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions