Skip to content

Instantly share code, notes, and snippets.

@aiya000
Created February 20, 2021 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiya000/b1b03d39b0d932362197c9d1fe37d80b to your computer and use it in GitHub Desktop.
Save aiya000/b1b03d39b0d932362197c9d1fe37d80b to your computer and use it in GitHub Desktop.
TypeScriptは`interface Foo { ... }`と`type Foo = { ... }`を区別するっぽい!?
/**
* Proves that A and B is the same types.
*/
export type Equal<A, B> = A extends B ? (B extends A ? true : never) : never
/**
* Makes all fields of A to `<its-type> | null`.
*/
export type Nullable<A extends Record<string, unknown>> = { [K in keyof A]: A[K] | null }
type Expected = { x: number | null }
// OK
const _proofNullable: Equal<Nullable<{ x: number }>, Expected> = true
type Foo = {
x: number
}
// OK
const _proofNullable1: Equal<Nullable<Foo>, Expected> = true
interface FooI {
x: number
}
// 2344: Type 'FooI' does not satisfy the constraint 'Record<string, unknown>'. Index signature is missing in type 'FooI'.
const _proofNullable0: Equal<Nullable<FooI>, Expected> = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment