-
-
Notifications
You must be signed in to change notification settings - Fork 0
type expect
Nicholas Berlette edited this page Jun 19, 2025
·
1 revision
function expectType<T>(value: T): void;A no-op function that is used for explicit type checking in TypeScript.
This function is a generic function that takes a single type parameter T and a
single argument of type T. The function does not perform any operations at
runtime, but it is used to check that the type of the argument passed to it is a
match for type T. This is useful for testing purposes.
| Name | Info |
|---|---|
value |
THe value to assert the type of. |
Nothing.
Testing
import { expectType } from "@nick/is/type/expect";
let result: string | number = "hello world".toString();
// This will raise a type error if `result` is not a `string`
expectType<string>(result);
const literal = "hello world";
// This will raise a type error if `literal` is not a string literal
expectType<"hello world">(literal);export type ExpectType<TExpected, TActual extends TExpected> = TActual;Type-level equivalent to the
expectType function.
This type alias accepts 2 type parameters: TExpected and TActual. If the
type of TActual is not assignable to TExpected, it will raise a compiler
error indicating as much. This is useful for testing purposes.
TExpected-
TActualextendsTExpected
Types