gristlabs / ts-interface-builder

Compile TypeScript interfaces into a description that allows runtime validation
Apache License 2.0
132 stars 28 forks source link

Duplicated variables when doing `--inline-imports` #14

Open lenowng opened 5 years ago

lenowng commented 5 years ago

TS source:

Car-type.ts

export interface Manufacturer {
  name: string;
  country: string;
  heavyDutyTruckSpecialize: boolean;
  ..
}

export interface Car {
  manufacturer: Manufacturer;
  engine: string;
}

Bike-type.ts

export interface Manufacturer {
  name: string;
  country: string;
  ..
}

export interface Bike {
  manufacturer: Manufacturer;
  engine: string;
}

Vehicle-type.ts

import { CarType as Car } from './Car-type.ts'
import { BikeType as Bike } from './Bike-type.ts'
export interface Vehicle {
  type: CarType | BikeType;
}

Above scenario is sketched out to brings out the idea of the possible problem. Building interface type from Vehicle-type.ts with --inline-imports will lead to having duplicated variable named Manufacturer being generated in the same type file because import alias was not respected.

Suggestion: Use the import alias a prefix to generated variable name.

darkalor commented 2 years ago

The above example could likely be handled by just naming the manufacturer type differently between the files. Here's a simpler and more broken example that will result in the same error, even for a single Manufacturer type. Haven't really found a workaround for this

// common.ts
export interface Manufacturer {
  name: string;
  country: string;
  ..
}

// engine.ts
export interface Engine {
  manufacturer: Manufacturer;
  engine: Engine;
}

// engine.ts
export interface Engine {
  name: String
  manufacturer: Manufacturer;
}
ismakutl commented 1 year ago

Any updates on this?

darkalor commented 1 year ago

FWIW my workaround ended up being something like this:

// validators.ts
const checkers = createCheckers(
  // EVERYTHING goes here
  fooSpecs,
  barSpecs
  commonSpecs
)

export default checkers
// foo.ts
import validator from 'utils/validator'
const FooValidator = validator.Foo

Essentially doing createCheckers once with everything, rather than making separate ones, which avoided multiple repeated definitions of the same thing. This doesn't fully solve the problem but it was sufficient for my use case

dsagal commented 1 year ago

@fastfrwrd, since you added the --inline-imports option, do you have any suggestions here?