TypeScript · Lesson 114 of 120
Reading Library Types
Understand .d.ts.
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.

- 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.
| key | value |
|---|---|
| First | name and <T> |
| Then | each parameter |
| Then | the 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