Created
February 20, 2021 15:08
-
-
Save aiya000/b1b03d39b0d932362197c9d1fe37d80b to your computer and use it in GitHub Desktop.
TypeScriptは`interface Foo { ... }`と`type Foo = { ... }`を区別するっぽい!?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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