type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

`JsonValue` can receive `type` but not `interface`

Open fregante opened this issue 6 months ago • 1 comments

import {JsonValue} from "type-fest";

declare function identity(value: JsonValue): JsonValue;

interface InterfaceObject {
    origin: string;
}

const objectViaInterface: InterfaceObject = {
    origin: 'red'
};

identity(objectViaInterface);
// Argument of type 'InterfaceObject' is not assignable to parameter of type 'JsonValue'.
//   Type 'InterfaceObject' is not assignable to type 'JsonObject'.
//    Type 'InterfaceObject' is not assignable to type '{ [x: string]: JsonValue; }'.
//      Index signature for type 'string' is missing in type 'InterfaceObject'.(2345)

type TypeObject = {
    origin: string;
}

const objectViaType: TypeObject = {
    origin: 'red'
};

identity(objectViaType) // Fine

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbwFIGcIDsBqBDANgVwFMBfOAMyghDgCIYBPMQgWjMJRhoG4AoHgE0IBjXNiiFy+dEJjAMcYIPSyGACgBueIgC44qDDgKEAlLv1YthXj2DLCUMtiESAknYdPCAeQBGAK2F4BB44ULhoYABzW10OKFtI3mI+IQwOcP9AzGBsNxh7R2ddPILPXwCZOABeRBCwiOj0XQBycX5mnmJrRUJlYDUITJls3PdCkz4GJjgAFUZvIfga4LDw+MbYmHj0RM6UtPhBipgRuaZdM4Xj6trVhpi4VsJ2zu6lFXpVI6ycy+MgA

Jsonify "fixes" the issue in some cases but it breaks in other

Generic issue for:

  • https://github.com/sindresorhus/type-fest/issues/547
  • https://github.com/sindresorhus/type-fest/issues/397

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

fregante avatar Feb 15 '24 08:02 fregante

In my case I'm using extends OmitIndexSignature<JsonValue> and that silenced the issue for now.

fregante avatar Feb 15 '24 08:02 fregante