Back to all snippets
Library/TypeScript/Strict Extract Type
typescriptintermediatetypesutilitiesvalidation

How to implement Strict Extract Type in Typescript

Type-safe extraction with compile-time validation

Quick Answer

`StrictExtract<T, U extends T>` is a drop-in replacement for `Extract` that errors at compile time if U is not actually a member of T.

Code Snippet

1type StrictExtract<T, U extends T> = Extract<T, U>;
2
3// Usage
4type Status = 'pending' | 'approved' | 'rejected';
5type PositiveStatus = StrictExtract<Status, 'approved'>;
6
7// This will error at compile time
8// type Invalid = StrictExtract<Status, 'invalid'>;

What is Strict Extract Type?

The built-in Extract<T, U> silently returns never if U is not a member of T, which hides typos and mistakes. StrictExtract adds a constraint (U extends T) so that attempting to extract a value that does not exist in the union produces a compile-time error immediately.

How It Works

  1. 1Add `U extends T` as a constraint on the second type parameter.
  2. 2Delegate to the built-in `Extract<T, U>` — the constraint does all the safety work.
  3. 3Any call with a U that is not a member of T will fail at compile time.

Common Use Cases

  • Status filtering - Safely narrow a Status union to specific variants
  • Event type subsets - Extract a subset of event names from a full union
  • Role-based types - Derive admin-only roles from a full roles union
  • Feature flags - Extract enabled flags from a flags union type

Key Benefits

  • Fails loudly at compile time instead of silently returning never
  • Makes incorrect extractions immediately visible in the IDE
  • Drop-in replacement for Extract with stricter safety
  • Zero runtime cost — purely a compile-time constraint

Common Mistakes to Avoid

  • Using the built-in `Extract` when you expect a specific subset — typos silently produce `never`.
  • Passing a union as U when you only need one member — use a specific literal for maximum safety.
  • Expecting StrictExtract to work when T is `any` — any satisfies all constraints.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more typescript examples

Frequently Asked Questions

When should I use StrictExtract vs a direct type annotation?

Use StrictExtract when deriving a sub-union programmatically from a base union type you do not own. Use a direct type annotation when the sub-union is stable and small enough to write out by hand.

About This Typescript Code Snippet

This free typescript code snippet for strict extract type is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this intermediate-level example will help you implement strict extract type quickly and correctly.

All snippets in the Snippetly library follow typescript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses typescript.

Tags: types, utilities, validation  | Language: typescript  | Difficulty: intermediate  | Category: TypeScript

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.