Creators to follow

N
nadia_ux44 followers
T
tomasz_forms44 followers
D
david_ops41 followers
AboutTermsPrivacyHelp

© 2026 teliprompt

MA
marcus_react
3 months ago•
Coding

TypeScript type narrowing cheat sheet with runnable examples

Claude Opus 4.6
code output
#typescript
#type-narrowing
#cheat-sheet
#generics
Views
10.8K
Copies
1.6K
Likes
1.6K
Comments
0
Copy rate
15.0%

Prompt

You are a TypeScript language expert. Produce a single .ts file that is a runnable cheat sheet of every major type-narrowing technique.

Target TypeScript version: [ts_version]
Audience: [audience]

Cover at minimum:
1. `typeof` guards for primitives
2. `instanceof` guards for classes
3. `in` operator for optional members
4. Equality narrowing (`===` literal, discriminated unions)
5. Truthiness narrowing with `NonNullable` pitfalls
6. User-defined type predicates (`x is Foo`)
7. `asserts` functions
8. `satisfies` operator for literal types
9. Exhaustiveness checks with `never`
10. Narrowing inside `.filter()` using predicates
11. Template literal types as compile-time narrowing
12. Const type parameters (TS 5.0+)

For each technique include:
- A short comment header naming the technique
- A minimal but real example
- A demonstration that unknown input is safely narrowed

Output format:
- Single compilable `narrowing-cheatsheet.ts` file
- Use `// --- N: Title ---` dividers
- Strict mode assumed; no `any`
- Finish with a `main()` that invokes each sample and `console.log`s a one-line result per section

Customise this prompt

Fill in 2 variables to personalise this prompt

Preview
You are a TypeScript language expert. Produce a single .ts file that is a runnable cheat sheet of every major type-narrowing technique. Target TypeScript version: [ts_version] Audience: [audience] Cover at minimum: 1. `typeof` guards for primitives 2. `instanceof` guards for classes 3. `in` operator for optional members 4. Equality narrowing (`===` literal, discriminated unions) 5. Truthiness narrowing with `NonNullable` pitfalls 6. User-defined type predicates (`x is Foo`) 7. `asserts` functions 8. `satisfies` operator for literal types 9. Exhaustiveness checks with `never` 10. Narrowing inside `.filter()` using predicates 11. Template literal types as compile-time narrowing 12. Const type parameters (TS 5.0+) For each technique include: - A short comment header naming the technique - A minimal but real example - A demonstration that unknown input is safely narrowed Output format: - Single compilable `narrowing-cheatsheet.ts` file - Use `// --- N: Title ---` dividers - Strict mode assumed; no `any` - Finish with a `main()` that invokes each sample and `console.log`s a one-line result per section

Example output

Related prompts

Async iterators and generators in modern JavaScript: a working guideby @olivia_test
Coding
CLI tool builder with argument parsing and colored outputby @nikolai_rust
Coding
Convert JavaScript to strict TypeScript with full type safetyby @marcus_react
Coding
Legacy JavaScript to modern ES2023 refactorby @marcus_react
Coding