Skip to content

Implement addition in the type system (TS 4.1) #185

@danvk

Description

@danvk

Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.

Detailed solution/guide is not required, but please be sure the challenge is solvable.

Info

difficulty: hard # medium / hard / extreme
title: Implement addition in the type system
#tags: tuple # separate by comma

Question

In addition to string literal types, TypeScript supports numeric literal types:

type One = 1
type TwoOrThree = 2 | 3

If you have two numbers, it's natural to want to add them! For example:

type T1 = Add<1, 2>          // should be 3
type T2 = Add<2, 3 | 4>      // should be 5 | 6
type T3 = Add<1 | 2, 3 | 4>  // should be 4 | 5 | 6

Template

type Add<A extends number, B extends number> = any

Test Cases

import { Equal, Expect, ExpectFalse, NotEqual } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Add<1, 2>, 3>>,
  Expect<Equal<Add<2, 3 | 4>, 5 | 6>>,
  Expect<Equal<Add<1 | 2, 3 | 4>, 4 | 5 | 6>>,
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    new-challengePropose a new challenge, a PR will be auto generatedts4.1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions