Skip to content

FnChainΒ #50991

@loynoir

Description

@loynoir

Suggestion

πŸ” Search Terms

  • strict Parameters<T>
  • associated ReturnType<T>
  • curry function

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Given

type MP2 = {
  a1: "a2";
  b1: "b2";
};
type MP3 = {
  a1: "a3";
  b1: "b3";
};
type MR = {
  a1: "ar";
  b1: "br";
};
function foo<K extends keyof MP2>(p1: K, p2: MP2[K], p3: MP3[K]): MR[K] {
  return { p1, p2, p3 } as any;
}
type FooFnChain = FnChain<typeof foo>;

type FooFnChainExpected =
  | { params: ["a1", "a2", "a3"]; result: "ar" }
  | { params: ["b1", "b2", "b3"]; result: "br" };

πŸ“ƒ Motivating Example

// [p1: keyof MP2, p2: "a2" | "b2", p3: "a3" | "b3"]
type FooParameters = Parameters<typeof foo>;
// "ar" | "br"
type FooReturn = ReturnType<typeof foo>;

// @ts-expect-error
foo('a1','b2','b3')

// no error
const a:FooParameters = ['a1', 'b2', 'b3']
void a

// no error
curry(foo)('a1')('b2')('b3')

// "ar" | "br"
const r = curry(foo)('a1')('a2')('a3')

πŸ’» Use Cases

// @ts-expect-error
foo('a1','b2','b3')

// @ts-expect-error
const a:FnChain<typeof foo>['params'] = ['a1', 'b2', 'b3']
void a

// @ts-expect-error
curry(foo)('a1')('b2')('b3')

// "ar"
const r = curry(foo)('a1')('a2')('a3')

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions