Skip to content

Instantly share code, notes, and snippets.

@safareli
Created May 28, 2021 15:08
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 safareli/fc5dffd1e88f0cc6f5362b65bc5b0343 to your computer and use it in GitHub Desktop.
Save safareli/fc5dffd1e88f0cc6f5362b65bc5b0343 to your computer and use it in GitHub Desktop.
let zz: {}
zz = {} // COMPILES
zz = 1 // COMPILES
zz = "1" // COMPILES
zz = true // COMPILES
zz = { ff:true } // COMPILES
zz = [11] // COMPILES
zz = [] // COMPILES
zz = null // ERROR: Type 'null' is not assignable to type '{}'.(2322)
zz = undefined // ERROR: Type 'undefined' is not assignable to type '{}'.(2322)
type RecordEmpty = Record<string,never>
let kk: RecordEmpty
kk = {} // COMPILES
kk = 1 // ERROR: Type 'number' is not assignable to type 'Record<string, never>'.(2322)
kk = "1" // ERROR: Type 'string' is not assignable to type 'Record<string, never>'.(2322)
kk = true // ERROR: Type 'boolean' is not assignable to type 'Record<string, never>'.(2322)
kk = { ff: true } // ERROR: Type 'boolean' is not assignable to type 'never'.(2322)
kk = [11] // ERROR: Type 'number' is not assignable to type 'never'.(2322)
kk = [] // ERROR: Type 'never[]' is not assignable to type 'RecordEmpty'. Index signature is missing in type 'never[]'.(2322)
kk = null // ERROR: Type 'null' is not assignable to type 'RecordEmpty'.(2322)
kk = undefined // ERROR: Type 'undefined' is not assignable to type 'RecordEmpty'.(2322)
declare let r0: {}
declare let r1: Omit<{a:never},"a">
declare let r2: Record<never,never>
declare let r3: RecordEmpty
if(typeof r0 === "number") { r0 } // r0: number
if(typeof r1 === "number") { r1 } // r1: number
if(typeof r2 === "number") { r2 } // r2: number
if(typeof r3 === "number") { r3 } // r3: never
r0 = 1 // COMPILES
r1 = 1 // COMPILES
r2 = 1 // COMPILES
r3 = 1 // ERROR: Type 'number' is not assignable to type 'Record<string,never>'.(2322)
/**
* A better alternative to type `{}`.
*
* When using `{}` you might think that it's just an empty Record,
* but in reality it it's just any nullable type see:
* ```ts
* let zz: {}
* zz = {} // COMPILES
* zz = 1 // COMPILES
* zz = "1" // COMPILES
* zz = true // COMPILES
* zz = { ff:true } // COMPILES
* zz = [11] // COMPILES
* zz = [] // COMPILES
* zz = null // ERROR: Type 'null' is not assignable to type '{}'.(2322)
* zz = undefined // ERROR: Type 'undefined' is not assignable to type '{}'.(2322)
* ```
*
* vs:
*
* ```ts
* let kk: RecordEmpty
* kk = {} // COMPILES
* kk = 1 // ERROR: Type 'number' is not assignable to type 'Record<string, never>'.(2322)
* kk = "1" // ERROR: Type 'string' is not assignable to type 'Record<string, never>'.(2322)
* kk = true // ERROR: Type 'boolean' is not assignable to type 'Record<string, never>'.(2322)
* kk = { ff: true } // ERROR: Type 'boolean' is not assignable to type 'never'.(2322)
* kk = [11] // ERROR: Type 'number' is not assignable to type 'never'.(2322)
* kk = [] // ERROR: Type 'never[]' is not assignable to type 'RecordEmpty'. Index signature is missing in type 'never[]'.(2322)
* kk = null // ERROR: Type 'null' is not assignable to type 'RecordEmpty'.(2322)
* kk = undefined // ERROR: Type 'undefined' is not assignable to type 'RecordEmpty'.(2322)
* ```
* export type RecordEmpty = Record<string, never>;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment