Skip to content

StrictExclude

Since 1.0.0

Strictly exclude types.

Signature

typescript
type StrictExclude<T, U extends T> = T extends U ? never : T

Parameters

ParameterDescription
TThe source type
UThe type to exclude (must be a subtype of T)

Description

Similar to TypeScript's built-in Exclude, but requires U to be a subtype of T, providing stricter type checking.

Examples

Basic Usage

typescript
import type { StrictExclude } from 'uni-types'

type Status = 'pending' | 'active' | 'inactive' | 'deleted'

type NonPending = StrictExclude<Status, 'pending'>
// 'active' | 'inactive' | 'deleted'

With Union Types

typescript
type Event = {
  type: 'click'
  x: number
  y: number
} | {
  type: 'scroll'
  scrollTop: number
} | {
  type: 'keypress'
  key: string
}

type NonClickEvent = StrictExclude<Event, { type: 'click' }>
// { type: 'scroll'; scrollTop: number } | { type: 'keypress'; key: string }

Released under the MIT License.