Skip to content

Type check errors when loading from json file #2431

@shot-codes

Description

@shot-codes

openapi-typescript version

7.9.1

Node.js version

24.5.0

OS + version

NixOS 25.11

Description

I'm trying to load a request body from a json file. When I do that i get type errors on the assignment to a variable that is annotated with the corresponding type. If I manually construct the json inline typescript is totally happy. The error I get is Type 'string' is not assignable to type '"bar"'. I can try to provide more information if it will help. But I can't share my actual types/api so it will require a little bit of work.

<script lang="ts">
    import type { components } from "./mySchema";

    // Typescript is happy here
    let x: components['schema']['MyType'] = { foo: "bar" }

    // Typescript gives me type errors here
    import data from './example.json';
    let x: components['schema']['MyType'] = data
</script>

example.json:

{ "foo": "bar" }

Reproduction

If needed I can try to make a minimal reproduction. But I first wanted to hear if this is expected/trivial to solve.

Expected result

Expecting no type errors

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions