Skip to content

Instantly share code, notes, and snippets.

@freddi301
Created July 16, 2025 13:34
Show Gist options
  • Select an option

  • Save freddi301/2175fce71632571e3f06b575ab78eb73 to your computer and use it in GitHub Desktop.

Select an option

Save freddi301/2175fce71632571e3f06b575ab78eb73 to your computer and use it in GitHub Desktop.
Typescript exact type
type Exact<A, B extends A> = A extends B
? B
: B extends Record<any, any>
? { [K in keyof B]: K extends keyof A ? Exact<A[K], B[K]> : never }
: B extends Array<any>
? { [K in keyof B]: K extends keyof A ? Exact<A[K], B[K]> : never }
: never;
function invariantize<A, R>(
fun: (a: A) => R
): <B extends A>(a: Exact<A, B>) => R {
return fun as any;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment