microsoft / microsoft/TypeScript

Dependent arguments assignability to overloaded functions

Open
#52,303 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Suggestion

🔍 Search Terms

overloads, dependent, tuple signatures

✅ 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

It would be like if dependent parameters (declared using tuple types) could satisfy matching overloads

📃 Motivating Example

TS playground

import * as React from 'react'

type Brand<T, Tag extends string> = T & { __tag: Tag };

export type UniqueNodeId = Brand<string, 'UniqueNodeId'>;
export type UniqueEdgeId = Brand<string, 'UniqueEdgeId'>;

type Dispose = () => void;

interface Observe {
  (type: 'node', uniqueId: UniqueNodeId, element: Element): Dispose;
  (type: 'edge', uniqueId: UniqueEdgeId, element: Element): Dispose;
}

const ctx = React.createContext<Observe>(() => () => void 0);

export function useResize(
  ...[type, uniqueId, ref]:
    | [type: 'node', uniqueId: UniqueNodeId, ref: React.RefObject<Element>]
    | [type: 'edge', uniqueId: UniqueEdgeId, ref: React.RefObject<Element>]
) {
  const observe = React.useContext(ctx)

  React.useLayoutEffect(() => {
    const element = ref.current!;
    return observe(type, uniqueId, element); // this is currently an error
  }, [type, uniqueId, ref, observe]);
}

💻 Use Cases

It seems like a natural extension of the dependent parameters improvements that shipped in TS 4.6. Overloads are often at odds with some TS features though (like with conditional types) so I'm not sure if you want to work on improvements related to them.

The nice thing about overloads is that they don't require extra binding patterns etc to be used - whereas declaring parameters with tuple types pollutes the runtime code and displays differently in the IDE tooltips etc (although sometimes the tooltips act as if the tuple-based signature would be an overloaded one).

Perhaps it would be good to have a documentation page explaining the difference, cons and pros of each approach? It also could suggest which one is better is better in the "modern" TS.

I can somewhat workaround this (TS playground) but the slight problem with this solution is that it requires me to reorder the parameters (to what I think is less intuitive to the caller, in my context). I could use tuples with trailing fixed elements but there is no good runtime way of unpacking such elements that wouldn't require extra set of helpers.

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 example and compare the dependent tuple-parameter call with the overloaded Observe signature. Investigate how overload assignability is checked and define completion as accepting the matching node and edge calls without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.