Skip to content

Instantly share code, notes, and snippets.

@reyronald
Created December 30, 2022 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyronald/2bfcd52fab7ab9d2737a3f16fb040562 to your computer and use it in GitHub Desktop.
Save reyronald/2bfcd52fab7ab9d2737a3f16fb040562 to your computer and use it in GitHub Desktop.
type ValidateShape<T, Shape> =
T extends Shape ?
Exclude<keyof T, keyof Shape> extends never ?
T : never : never;
type Exact<A, B> =
A extends B ?
B extends A ? A
: never
: never;
function Test<T>(_args: ValidateShape<T, { test: string }>) {
//
}
// goood
Test({ test: "haha" });
const o1= { test: "haha" }
Test(o1);
const o2= { test: "haha" } as const
Test(o2);
const o3 = Object.freeze({ test: "haha" })
Test(o3);
/// error
Test({ test: "haha", other: "asdf" });
const oe1= { test: "haha", other: "asdf }
Test(oe1);
const oe2 = { test: "haha", other: "asdf" } as const;
Test(oe2);
const oe3 = Object.freeze({ test: "haha", other: "asdf" })
Test(oe3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment