microsoft / microsoft/TypeScript

Type Math.min & Math.max using generic

Open
#30,924 9 comments 16 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

  • Math.min generic
  • Math.max generic

Suggestion

Currently, Math.min is typed as:

min(...values: number[]): number;

However, I would like to change the definition to:

min<T extends number>(...values: [T, ...T[]]): T;
min(): number; // Because this will return Infinity

Because Math.min should never return things that aren't its input. (Except when non-number is passed in, then it'll return NaN. But that's impossible with this typing.)

(Okay, there's another case: if no value is passed in, it'll return Infinity. Unfortunately, there's no literal type for Infinity so we can't type that correctly. And AFAIK there's no way to force at least 1 parameter with rest args. Updated: there is, using tuple type: [T, ...T[]]. Proposal updated. Still, it would be nice to have literal Infinity type.)

(The same applies with Math.max, except Infinity is now -Infinity)

Use Cases

Let's say I have this type:

type TBankNoteValues = 1 | 5 | 10 | 20 | 50 | 100;

And I want to know what is the highest-value banknote I have in the array. I could use Math.max to find that out. But with the current typing, the return value isn't guaranteed to be TBankNoteValues.

let values: TBankNoteValues[] = [50, 100, 20];
let maxValues = Math.max(...values); // number

And now I can't pass maxValues to a function that expects TBankNoteValues anymore.

Examples

type TBankNoteValues = 1 | 5 | 10 | 20 | 50 | 100;

let values: TBankNoteValues[] = [50, 100, 20];
let maxValues = Math.max(...values);
// Current type: number
// Expected type: TBankNoteValues

Playground link

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • Shouldn't be any. Anything that expects the old signature, TypeScript should just infer T to number.
    • Now that I think about it, this make the return type of the function narrows down. So, it could make type inference on a variable declaration changes unexpectedly. An explicit type annotation should fix this.
  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 by reproducing the bank-note example in the linked TypeScript Playground and reviewing the Math.min and Math.max declarations described in the issue. Compare the proposed generic tuple signatures with existing TypeScript behavior, including zero-argument calls; done means the design is validated and its compatibility implications are resolved.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.