Skip to content

Ts.VariancePhantom

Ts / VariancePhantom

Phantom type helper that makes a type parameter covariant.

Import

typescript
import { Ts } from '@wollybeard/kit'

// Access via namespace
Ts.VariancePhantom.someFunction()
typescript
import * as Ts from '@wollybeard/kit/ts'

// Access via namespace
Ts.VariancePhantom.someFunction()

Types

[T] Co

typescript
type Co<$T> = () => $T

Phantom type helper that makes a type parameter covariant.

Examples:

typescript
interface 
Container
<
T
> {
readonly
__type
?:
Covariant
<
T
>
} let
narrow
:
Container
<1> = {}
let
wide
:
Container
<number> = {}
wide
=
narrow
// ✅ Allowed (1 extends number)
narrow
=
wide
// ❌ Error (number does not extend 1)

[T] Contra

typescript
type Contra<$T> = (value: $T) => void

Phantom type helper that makes a type parameter contravariant.

Examples:

typescript
interface 
Handler
<
T
> {
readonly
__type
?:
Contravariant
<
T
>
} let
narrow
:
Handler
<1> = {}
let
wide
:
Handler
<number> = {}
narrow
=
wide
// ✅ Allowed (reversed direction!)
wide
=
narrow
// ❌ Error

[T] In

typescript
type In<$T> = (value: $T) => $T

Phantom type helper that makes a type parameter invariant.

Examples:

typescript
interface 
Exact
<
T
> {
readonly
__type
?:
Invariant
<
T
>
} let
one
:
Exact
<1> = {}
let
num
:
Exact
<number> = {}
num
=
one
// ❌ Error (no direction works)
one
=
num
// ❌ Error (no direction works)

[T] Bi

typescript
type Bi<$T> = { bivariantHack(value: $T): void }['bivariantHack']

Phantom type helper that makes a type parameter bivariant (unsafe).

Examples:

typescript
interface 
Unsafe
<
T
> {
readonly
__type
?:
Bivariant
<
T
>
} let
one
:
Unsafe
<1> = {}
let
num
:
Unsafe
<number> = {}
num
=
one
// ⚠️ Allowed (both directions work)
one
=
num
// ⚠️ Allowed (unsafe!)