Skip to content
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

partial application typing fail with custom error type #46699

Closed
samuela opened this issue Nov 5, 2021 · 2 comments
Closed

partial application typing fail with custom error type #46699

samuela opened this issue Nov 5, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@samuela
Copy link

samuela commented Nov 5, 2021

Bug Report

🔎 Search Terms

partial application, currying, type inference, higher-order functions, polymorphism

🕗 Version & Regression Information

Affects typescript@4.4.4 and v4.5.0-beta.

  • I was unable to test this on prior versions because of the following error:
❯ npm i -D typescript@next
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! Found: typescript@4.6.0-dev.20211105
npm ERR! node_modules/typescript
npm ERR!   dev typescript@"4.6.0-dev.20211105" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peerOptional typescript@">=3.3.1" from eslint-config-next@12.0.2
npm ERR! node_modules/eslint-config-next
npm ERR!   dev eslint-config-next@"12.0.2" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /Users/skainswo/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/skainswo/.npm/_logs/2021-11-05T19_54_53_547Z-debug.log

⏯ Playground Link

Playground link with relevant code

💻 Code

// See https://github.com/Microsoft/TypeScript/issues/7556.
type ErrorT = { new (message: string): Error };

function _notNull<T>(errorType: ErrorT, v: T | null | undefined): T {
  if (v === null || v === undefined) {
    throw new errorType("error message");
  }
  return v;
}

const partial =
  <A, B, T>(f: (a: A, b: B) => T, a: A) =>
  (b: B) =>
    f(a, b);

function partial2<A, B, T>(f: (a: A, b: B) => T, a: A) {
  return (b: B) => f(a, b);
}

// good: (b: number) => number
const sumPartial = partial((a: number, b: number) => a + b, 5);
const sumPartial2 = partial2((a: number, b: number) => a + b, 5);

// good: <T>(v: T | null | undefined) => T
const notNull = <T>(v: T | null | undefined) => _notNull(Error, v);

// bad: (v: unknown) => unknown
const notNull2 = _notNull.bind(null, Error);
const notNull3 = partial(_notNull, Error);
const notNull4 = partial2(_notNull, Error);

export {};

🙁 Actual behavior

_notNull.bind(null, Error), partial(_notNull, Error), and partial2(_notNull, Error) are typed unhelpfully as (v: unknown) => unknown. I can understand that bind is messy to infer types on, but I don't understand the trouble with partial and partial2 which are defined with simple type signatures.

🙂 Expected behavior

For everything to have the type <T>(v: T | null | undefined) => T.

@andrewbranch
Copy link
Member

Duplicate of #30727 (different example, same problem)

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 10, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants