microsoft / microsoft/TypeScript

potential bug related to generics

Open
#55,173 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

generics
pipe
functional programming

🕗 Version & Regression Information

5.2.0-dev

  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
export type Func = (..._: any[]) => unknown;

export type Unary<Input, Output> = (_: Input) => Output;

export type Tail<L extends unknown[]> = L extends
  readonly [unknown, ...infer LTail] ? LTail
  : L;

type UnaryFn<A, R> = (a: A) => R;
type Arg<F> = F extends UnaryFn<infer A, unknown> ? A : never;
type Res<F> = F extends UnaryFn<any, infer R> ? R : never;

type ValidCompose<F1, F2> = Res<F1> extends (Arg<F2> | Promise<Arg<F2>>) ? F1
  : (arg: Arg<F1>) => Arg<F2>;

type ValidPipe<FS> = FS extends [infer F1, infer F2, ...infer Rest]
  ? [ValidCompose<F1, F2>, ...ValidPipe<[F2, ...Rest]>]
  : FS;

export const pipe = <Fs extends Func[]>(  ...fs: ValidPipe<Fs>) => {}

(<T, Fn extends (x: T) => number>(f: Fn) => {
  const g = (x: T) => x;
  const h = (_: T) => 1;
  pipe(g, f);  // This gives a typing error.
  pipe(g, h);  // This works fine.
});
🙁 Actual behavior

The first pipe invocation results in a typing error.

🙂 Expected behavior

None should have an error, because the generic typing ensures everything is in order.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and reduce the generic pipe example around ValidCompose, ValidPipe, and the two pipe invocations. Investigate the type-checking behavior for the generic function f versus h; done means both calls type-check without errors while preserving the reported generic constraints.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.