Skip to content

Block-scoped variable 'T' used before its declaration dependent on input order #45

@FauxFaux

Description

@FauxFaux

An input of:

interface A {
  b: B
}
interface B {
  x: number;
}

Generates this typebox:

import { Type, Static } from "@sinclair/typebox";
type A = Static<typeof A>;
const A = Type.Object({
  b: B,
});
type B = Static<typeof B>;
const B = Type.Object({
  x: Type.Number(),
});

Which fails to compile with:

test/index.ts:5:6 - error TS2448: Block-scoped variable 'B' used before its declaration.

5   b: B,
       ~

I was hoping typebox-codegen would generate typescript code which would compile.

If you flip the declaration order in the input file, it works fine.

I'm pretty sure this is different to #31, despite having the same compile error.


A naive topological sort of the file fixes this for me, for my inputs, but this ends up being quite a bit of extra code, so I'm not sure it's the best approach.

That is, to generate code from my ts-proto output, containing four interfaces in three files, I have to:

  • babel/parser + babel/traverse to pull out all the imports, interfaces, their names, and their dependencies
  • rearrange the interfaces in the list such that they occur before their dependencies
  • .join('\n') the interfaces back together, and pass them to typebox-codegen
  • re-fix the imports

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions