Contents
Want to become a TypeScript pro? Master advanced TypeScript skills with these resources, from type definitions and challenging type puzzles to practical utility libraries and API development tools.
1. DefinitelyTyped โ A collection of type definitions
DefinitelyTyped serves as a community-driven collection of high-quality TypeScript type definitions.
The repository for high quality TypeScript type definitions.
The repository for high quality TypeScript type definitions.
You can also read this README in Espaรฑol, ํ๊ตญ์ด, ะ ัััะบะธะน, ็ฎไฝไธญๆ, Portuguรชs, Italiano, ๆฅๆฌ่ช and Franรงais!
Link to Admin manual
!!! Important! This repo has recently changed layout! !!!
Definitely Typed has recently changed to a proper pnpm
monorepo; you may want to reread this document for changes to the layout of packages in this repo.
At the very least, you may want to git clean -fdx
the repo (or node ./scripts/clean-node-modules.js
on Windows) to clean up node_modules
and run pnpm install --filter .
to install the workspace root. See further sections for more info on pnpm install
.
Current status
This section tracks the health of the repository and publishing process.
It may be helpful for contributors experiencing any issues with their PRs and packages.
In simple terms, it provides TypeScript interfaces and type information for JavaScript libraries that donโt have them built-in.
When youโre using a JavaScript library in a TypeScript project, TypeScript needs to know the shapes and types of the libraryโs exports to type-check your code correctly.
Itโs highly popular and has over 100 million weekly downloads!
This repo allows developers to use existing JavaScript libraries within their TypeScript projects seamlessly, ensuring type safety.
If the library doesnโt provide its own types, you can likely find them in DefinitelyTyped. These type definitions are then used by the TypeScript compiler to understand the libraryโs structure, offering auto-completion, type checking, and other IDE features for a smoother development experience.
Example (Lodash)
Letโs say youโre working on a TypeScript project and decide to use Lodash, a popular utility library. Lodash itself is written in JavaScript and doesnโt include TypeScript definitions.
Here’s how you can use Lodash with types in your project with the help of DefinitelyTyped:
First, install Lodash:
npm install lodash
Then, install the type definitions for Lodash from DefinitelyTyped:
npm install @types/lodash --save-dev
Now, you can use Lodash in your TypeScript file with full type support:
import _ from 'lodash';
// Example usage with full type support
let numArray: number[] = [1, 2, 3, 4, 5];
let sum: number = _.sum(numArray); // Lodash's sum function
console.log(sum); // Output will be 15
By using DefinitelyTyped’s type definitions, you can maintain type safety and take full advantage of TypeScript’s features, even when using JavaScript libraries.
2. Type Challenges โ A collection of TypeScript puzzles
The repo provides a collection of TypeScript puzzles similar to LeetCode problems.
Each challenge focuses on a specific aspect of TypeScriptโs type system, from basic concepts to complex type manipulation. Youโll often need to use generic types or apply advanced features like conditional types and mapped types to solve them.
Collection of TypeScript type challenges with online judge
These exercises will help you to test and improve your understanding of TypeScriptโs type system.
Example: First of Array
Letโs look at the โEasyโ level challenge. The task is to construct a generic type First that takes an array and returns its first element:
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
type head1 = First<arr1> // expected to be 'a'
type head2 = First<arr2> // expected to be 3
To solve the task you can use the Typescript playground
The solution might be something like this:
type First<T extends any[]> = T extends [infer First, ...infer Rest] ? First : never
-
T extends any[]
: We constrain the input T to be an array. -
T extends [infer First, ...infer Rest]
: Pattern matching to extract the first element as First and the rest of the array as Rest. -
First : never
: Conditional type for handling empty arrays.
The Type Challenges repository provides a structured and engaging way to level up your TypeScript mastery.
3. Utility types โ A collection of pre-written utility types
This collection of pre-written utility types saves you time and effort when working with different data types in TypeScript.
This repository offers a collection of utility types that can be applied in various TypeScript projects.
Collection of utility types, complementing TypeScript built-in mapped types and aliases (think “lodash” for static types).
Features
Goals
- Quality – thoroughly tested for type correctness with type-testing library
dts-jest
- Secure and minimal – no third-party dependencies
- No runtime cost – it’s type-level only
Installation
# NPM
npm install utility-types
# YARN
yarn add utility-types
Compatibility Notes
TypeScript support
-
v3.x.x
– TypeScript v3.1+ -
v2.x.x
– TypeScript v2.8.1+ -
v1.x.x
– TypeScript v2.7.2+
Funding Issues
Utility-Types is an open-source project created by people investingโฆ
These types exist solely at compile time, leaving no runtime cost in your final JavaScript code.
Example: TypeScript Typeguard isPrimitive
First, install utility-types:
npm install utility-types
Let’s use isPrimitive
Typeguard exampleโ-โa TypeScript Typeguard for the Primitive
type
This can be useful to control the type of a parameter as the program flows.
import { Primitive, isPrimitive } from 'utility-types';
const consumer = (param: Primitive[] | Primitive): string => {
if (isPrimitive(param)) {
// typeof param === Primitive
return String(param) + ' was Primitive';
}
// typeof param === Primitive[]
const resultArray = param
.map(consumer)
.map(rootString => '\n\t' + rootString);
return resultArray.reduce((comm, newV) => comm + newV, 'this was nested:');
};
Primitive
: This type represents the basic building blocks of JavaScript and TypeScript values: strings, numbers, booleans, etc.isPrimitive
: This type guard function lets you dynamically check if a given variable is a primitive type. This is especially valuable when working with data that could have varying structures.
Benefits of using utility-types:
-
Cleaner code: The
isPrimitive
type guard avoids manualtypeof
checks and potential branching. -
Type safety: It ensures that we’re only manipulating primitive values within the appropriate code block.
4. Typescript book โ Open-source e-book
Free and open-source e-book that dives deeply into TypeScript’s features. Perfect if you prefer a traditional book-like format.
You can freely access, read, and even contribute to the bookโs content.
The definitive guide to TypeScript and possibly the best TypeScript book . Free and Open Source
I’ve been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from Stack Overflow / DefinitelyTyped and general engagement with the TypeScript community. You can follow for updates and don’t forget to โ on GitHub
Reviews
- Thanks for the wonderful book. Learned a lot from it. (link)
- Its probably the Best TypeScript book out there. Good Job (link)
- Love how precise and clear the examples and explanations are! (link)
- For the low, low price of free, you get pages of pure awesomeness. Chock full of source code examples and clear, concise explanations, TypeScript Deep Dive will help you learn TypeScript development. (link)
- Just a big thank you! Best TypeScript 2 detailed explanation! (link)
- This gitbook got my project going pronto. Fluent easy read 5 starsโฆ
The book is known for its easy-to-understand explanations and illustrative examples. This makes it suitable for both beginners and experienced programmers who want to improve their TypeScript knowledge.
The book describes various aspects of TypeScript, from its core concepts and syntax to advanced topics like generics, decorators, and metaprogramming.
5. tRPC.io โ End-to-end typesafe API
tRPC offers a solution for building modern APIs with a focus on type safety and developer experience. This open-source project provides tools and libraries needed to construct type-safe APIs.
The definitive guide to TypeScript and possibly the best TypeScript book . Free and Open Source
I’ve been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from Stack Overflow / DefinitelyTyped and general engagement with the TypeScript community. You can follow for updates and don’t forget to โ on GitHub
Reviews
- Thanks for the wonderful book. Learned a lot from it. (link)
- Its probably the Best TypeScript book out there. Good Job (link)
- Love how precise and clear the examples and explanations are! (link)
- For the low, low price of free, you get pages of pure awesomeness. Chock full of source code examples and clear, concise explanations, TypeScript Deep Dive will help you learn TypeScript development. (link)
- Just a big thank you! Best TypeScript 2 detailed explanation! (link)
- This gitbook got my project going pronto. Fluent easy read 5 starsโฆ
tRPC integrates seamlessly with popular web frameworks such as React, Next.js and Express.js.
Type-safe APIs have several advantages:
- Reduced Errors: Static type checking helps catch potential errors at development time, preventing runtime issues that can be difficult to debug later.
- Improved Maintainability: A type-safe API provides a clear understanding of the data structures and interactions involved.
- Enhanced Developer Experience: Autocompletion and other IDE features powered by static types can significantly improve development speed and overall developer satisfaction.
Becoming a true TypeScript expert takes time and practice. These resources will help you build a strong start. Keep learning, keep trying new things, and connect with other TypeScript developers to take your skills to the next level.
This article was originally posted on Medium.
[fluentform id="8"]