Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jsnajdr opened this issue May 5, 2021 · 1 comment Β· Fixed by #58008
Closed

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

jsnajdr opened this issue May 5, 2021 · 1 comment Β· Fixed by #58008
Labels
Bug A bug in TypeScript
Milestone

Comments

@jsnajdr
Copy link

jsnajdr commented May 5, 2021

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

function withP1<P>(p: P) {
  const m = <I>(from: I) => ({ ...from, ...p });
  return m;
}

const addP1 = withP1({ foo: 1 });
const added1 = addP1({ bar: 2 });

console.log(added1.foo, added1.bar);

const createTransform = <I, O>(tr: (from: I) => O) => tr;

function withP2<P>(p: P) {
  const m = <I>(from: I) => ({ ...from, ...p });
  return createTransform(m);
}

const addP2 = withP2({ foo: 1 });
const added2 = 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:

declare const addP2: <I>(from: I) => I & P;

On the other hand, the addP1 version works well:

declare const addP1: <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.

@Andarist
Copy link
Contributor

I thought that it might have been just fixed by #57403 but it didn't πŸ˜… My suspicion is that this PR is crucial for fixing this - maybe it just missed something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants