Awesome Open Source
Awesome Open Source

📚 Meta-Typing

Functions and algorithms implemented purely with TypeScript's type system

Introduction

TypeScript's type system lets us catch bugs and errors in our code as we write it, instead of later on when the code runs. But... that's the obvious way to use the type system... 😜

This project attempts to push TypeScript's type system to its limits by actually implementing various functions and algorithms, purely on top of the type system.

Every implementation includes comments describing in detail what's going on. Some functions and algorithms use creative (and sometimes not officially supported) solutions to overcome some limitations of the type system.

☝ Please note that this project is meant to be used for fun and learning purposes and not for practical use.

showcase

Try running the code

Start by installing dependencies:

$ yarn

Open a file of any function or algorithm and hover over the types to see the results of "running" that function with some input (try hovering the resulting type).

You can also run tests (written with tsd) with:

$ yarn test

Functions and algorithms:

  • Math:

    • Basic arithmetic - add one and decrease by one.
    • Add - adds two numbers.
    • Subtract - subtracts two numbers.
    • Multiply - multiplies two numbers.
    • Divide - divides two numbers.
    • Greater than or equal - checks if a value is greater than or equal to another value.
    • Less than or equal - checks if a value is less than or equal to another value.
    • Max - computes the maximum value of an array.
    • Min - computes the minimum value of an array.
    • Sum - computes the sum of the values in array.
    • Remainder - return the remainder (%) when one number is divided by a second number.
  • Lists

    • Head - gets the first element of an array.
    • Tail - gets all but the first element of an array.
    • Reverse - Reverses an array so that the first element becomes the last, the second element becomes the second to last, and so on.
    • Size - gets the size of an array.
    • Concat - creates a new array by concatenating two arrays together.
    • Drop - creates a slice of an array with n elements dropped from the beginning.
    • Take - creates a slice of an array with n elements taken from the beginning.
    • Uniq - creates a duplicate-free version of an array.
    • Includes - checks if a value is an array.
    • IndexOf - gets the index at which the first occurrence of a value is found in an array.
    • Difference - creates an array of values from the first array that are not included in the other given arrays.
    • Intersection - creates an array of unique values that are included in all given arrays.
    • Slice - creates a slice of an array from start up to, but not including, end.
    • Flatten - flattens an array a single level deep.
    • Pull - removes all given values from an array.
    • Chunk - creates an array of elements split into groups the length of size.
    • Zip - creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
    • Range - creates an array of numbers progressing from start up to, but not including, end.
  • Sorting

    • Quick-sort - an efficient sorting algorithm.
    • Merge-sort - another efficient sorting algorithm.
    • Insertion-sort - a simple sorting algorithm that builds the final sorted array (or list) one item at a time.
  • Utility

    • IsEqual - compares between two values to determine if they are equivalent.
  • Puzzles

    • N-Queens - the problem of placing N chess queens on an N×N chessboard so that no two queens threaten each other.
    • Maze-solving - find the shortest path to solve a maze with obstacles.
    • Binary trees - a tree data structure in which each node has at most two children.
    • Square Matrix Rotation - you are given an N×N 2D matrix (representing an image). Rotate the matrix by 90 degrees (clockwise and counter-clockwise).
    • Towers of Hanoi - a mathematical game or puzzle that consists of three rods and a number of disks of different sizes.

Additional links


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
typescript (10,909) 
algorithms (443) 
learning (352) 
example (241) 
functional (147) 
recursion (29) 
type-system (16) 

Find Open Source By Browsing 7,000 Topics Across 59 Categories

Advertising 📦 10
All Projects
Application Programming Interfaces 📦 124
Applications 📦 192
Artificial Intelligence 📦 78
Blockchain 📦 73
Build Tools 📦 113
Cloud Computing 📦 80
Code Quality 📦 28
Collaboration 📦 32
Command Line Interface 📦 49
Community 📦 83
Companies 📦 60
Compilers 📦 63
Computer Science 📦 80
Configuration Management 📦 42
Content Management 📦 175
Control Flow 📦 213
Data Formats 📦 78
Data Processing 📦 276
Data Storage 📦 135
Economics 📦 64
Frameworks 📦 215
Games 📦 129
Graphics 📦 110
Hardware 📦 152
Integrated Development Environments 📦 49
Learning Resources 📦 166
Legal 📦 29
Libraries 📦 129
Lists Of Projects 📦 22
Machine Learning 📦 347
Mapping 📦 64
Marketing 📦 15
Mathematics 📦 55
Media 📦 239
Messaging 📦 98
Networking 📦 315
Operating Systems 📦 89
Operations 📦 121
Package Managers 📦 55
Programming Languages 📦 245
Runtime Environments 📦 100
Science 📦 42
Security 📦 396
Social Media 📦 27
Software Architecture 📦 72
Software Development 📦 72
Software Performance 📦 58
Software Quality 📦 133
Text Editors 📦 49
Text Processing 📦 136
User Interface 📦 330
User Interface Components 📦 514
Version Control 📦 30
Virtualization 📦 71
Web Browsers 📦 42
Web Servers 📦 26
Web User Interface 📦 210