sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
13.64k stars 515 forks source link

Nested structure without index signature is not assignable to `Jsonifiable` #778

Open bleistift-zwei opened 7 months ago

bleistift-zwei commented 7 months ago

Consider this simple nested data structure:

interface Inner {
  foo: string
}

interface Outer {
  bar: Inner
}

A value of type Outer can be losslessly converted to and from JSON. However, assigning it to a variable of type Jsonifiable won’t compile with TS 5.5.2 and type-fest@4.20.1 Playground link:

declare const outer: Outer
let jsonifiable: Jsonifiable = outer
//  ^ Type 'Outer' is not assignable to type 'Jsonifiable'.
//      Type 'Outer' is not assignable to type '{ [x: string]: Jsonifiable | undefined; }'.
//        Index signature for type 'string' is missing in type 'Outer'.(2322)

I believe the assignment should be valid.

Upvote & Fund

Fund with Polar

sindresorhus commented 7 months ago

// @itsjohncs