AI Hints
Home›TypeScript Tutorial›Habits and Library Types
TypeScript · Lesson 114 of 120

Reading Library Types

Understand .d.ts.

⏱ 2 min read▶ Video at 1:30:13🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

When you hover over a library function, you see its types. They can look scary, but they're made of the same pieces you've learned. Let's read one together.

Reading Library Types
From the video: Reading Library Types
  • Hover a function
  • Looks scary
  • Same pieces

cache.d.ts

This declares a generic cache. K extends string, and V defaults to unknown. Get returns V or undefined. Set takes an optional options object, with a time to live. And create cache is a generic function returning a cache.

cache.d.tstypescript
export interface Cache<K extends string, V = unknown> {
  get(key: K): V | undefined;
  set(key: K, value: V, options?: { ttlMs?: number }): void;
  keys(): readonly K[];
}

export declare function createCache<K extends string, V>(
  name: string,
): Cache<K, V>;

How to read them

Start with the name, and the type parameters. Then each parameter, one at a time. Then the return type. Question marks mean optional. And readonly means you shouldn't change it.

keyvalue
Firstname and <T>
Theneach parameter
Thenthe return type
Marks? optional, readonly

Summary

Library types are built from the same pieces you know. Read them piece by piece. Next, the cheat sheets.

Key takeaways
  • Type parameters
  • Parameters
  • Return type
Next up: Cheat Sheet: Basics — the essentials