What is ts-toolbelt?
The ts-toolbelt package provides a comprehensive set of type utilities to improve TypeScript typing and enable type transformations, checks, and more complex operations. It enhances TypeScript's static typing capabilities and helps in building more robust type-safe applications.
What are ts-toolbelt's main functionalities?
Object Manipulation
Merge two types into one. This is useful for combining properties from multiple objects into a single type.
{ type Merged = Merge<{name: string}, {age: number}>; // Result: {name: string, age: number} }
Type Checking
Check if two types are equivalent. This utility helps in validating type structures for consistency.
{ type check = Equals<{a: string}, {a: string, b: number}>; // Result: false }
Conditional Types
Apply conditional logic to types. This feature allows for conditional type assignments based on certain conditions.
{ type Conditional = If<true, 'Yes', 'No'>; // Result: 'Yes' }
Other packages similar to ts-toolbelt
utility-types
Provides a collection of utility types for TypeScript, similar to ts-toolbelt. While utility-types focuses more on transformations and mappings, ts-toolbelt offers a broader range of type operations including type checks and advanced manipulations.
type-fest
A package that includes a variety of utility types for TypeScript. Type-fest is similar to ts-toolbelt but tends to be simpler and more focused on everyday use cases, whereas ts-toolbelt provides more comprehensive and complex utilities for advanced type manipulation.
TypeScript's largest utility library
📖 Documentation
·
📣 Announcements
·
🐞 Report Bug
·
🍩 Request Feature
·
🤔 Ask Questions
About
ts-toolbelt ships utility types that provide simple ways to update, change, and compute TypeScript types.
It offers unique dynamic features, allowing you to write type-safe software more easily and in less time than you do today.
Its programmatic API brings new capabilities to TypeScript with +200 tested type utilities. This makes it the largest and the most reliable type collection out there.
It uses the type system itself for TypeScript to compute complex types. So its API exposes types that trade CPU & RAM for higher type safety.
Goals
- Answer the question to "How can I do this in TypeScript?"
- Provide a programmatic standard API for the type system
- Promote type evolution/reusability within your codebase
- Software that is more type-safe, flexible, and robust
- Bring a whole new set of extra features to TypeScript
- Extensively tested type utilities for maximum type safety
- This package aims to be the home of all utility types
- Answer questions about types and share knowledge
You'll find all the types you can ever need in this single and well organized package.
Getting Started
Prerequisites
npm install typescript@^4.1.0 --save-dev
For best results, add this to your tsconfig.json
{
"compilerOptions": {
"strictNullChecks": true,
"strict": true,
"lib": ["es2015"],
}
}
Installation
npm install ts-toolbelt --save
Hello World
import {Object} from "ts-toolbelt"
type merge = Object.Merge<{name: string}, {age?: number}>
type optional = Object.Optional<{id: number, name: string}, "name"}>
You can level-up, and re-code this library from scratch.
Imports
The project is organized around TypeScript's main concepts:
Any | Boolean | Class | Function | Iteration | List |
---|
Number | Object | Object.P | String | Union | Test |
TIP
How to choose categories? Match your type with them.
There are many ways to import the types into your project:
-
Explicit
import {Any, Boolean, Class, Function, Iteration, List, Number, Object, String, Union} from "ts-toolbelt"
-
Compact
import {A, B, C, F, I, L, N, O, S, U} from "ts-toolbelt"
-
Portable
import tb from "ts-toolbelt"
You can also import our non-official API from the community:
import {Community} from "ts-toolbelt"
TIP
The community API is for our community to publish useful types that don't see fit in the standard API.
Access older docs at https://millsp.github.io/ts-toolbelt/version/
EXAMPLE
https://millsp.github.io/ts-toolbelt/4.2.1/
In this wiki, you will find some extra resources for your learning, and understanding.
Are you missing something? Participate to the open-wiki by posting your questions.
Running tests
For this project
To run the lint
& type
tests, simply run:
npm test
For your project
Want to test your own types? Let's get started:
import {Number, Test} from "ts-toolbelt"
const {checks, check} = Test
checks([
check<Number.Plus<"1", "30">, "31", Test.Pass>(),
check<Number.Plus<"5", "-3">, "2", Test.Pass>(),
])
TIP
Place it in a file that won't be executed, it's just for TypeScript to test types.
Continuous Integration
The releases are done with Travis CI in stages & whenever a branch or PR is pushed:
- Tests are run with
npm test
- Tests against DefinitelyTyped
- Releases to npm@[branch-name]
Compatibility
The project is maintained to adapt to the constant changes of TypeScript:
ts-toolbelt | typescript |
---|
9.x.x | ^4.1.x |
Major version numbers will upgrade whenever TypeScript had breaking changes.
Otherwise, the release versions will naturally follow the semantic versioning.
What's next
-
Automated performance tests
npx tsc --noEmit --extendedDiagnostics
-
Need to write more examples
Related Projects
Name | Intro |
---|
eledoc | 🌒 A material dark theme for TypeDoc |
utility-types | Collection of utility types, complementing TypeScript built-in mapped types and aliases |
License