Error "Type has multiple definitions" thrown when types with same name are found
See original GitHub issueThe generator currently doesn’t like types in different files having the same name:
// test2.ts
export type Field = string | number;
// test.ts
import { Field as Field2 } from "./test2";
export type Field = string;
export type MyObject = {
a: Field;
b: Field2;
};
When generating the schema for MyObject
then the generator stops with this error:
/home/k/Projects/ts-json-schema-generator/dist/ts-json-schema-generator.js:33
throw error;
^
Error: Type "Field" has multiple definitions.
at /home/k/Projects/ts-json-schema-generator/dist/src/SchemaGenerator.js:69:23
at Array.reduce (<anonymous>)
at SchemaGenerator.appendRootChildDefinitions (/home/k/Projects/ts-json-schema-generator/dist/src/SchemaGenerator.js:66:14)
at /home/k/Projects/ts-json-schema-generator/dist/src/SchemaGenerator.js:21:44
at Array.forEach (<anonymous>)
at SchemaGenerator.createSchema (/home/k/Projects/ts-json-schema-generator/dist/src/SchemaGenerator.js:21:19)
at Object.<anonymous> (/home/k/Projects/ts-json-schema-generator/dist/ts-json-schema-generator.js:22:56)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Error with multiple definitions of function - c++ - Stack Overflow
The corresponding cpp file, which you will compile separately, will have the function definition.
Read more >ts-to-json - npm
Generate JSON schema from your Typescript sources. Latest version: 1.7.0, last published: 5 months ago. Start using ts-to-json in your ...
Read more >Symbol has multiple definitions error, but it doesnt...
If I change the name in this 1 location to anything else, it still comes back with the same error saying it has...
Read more >Error - JavaScript - MDN Web Docs - Mozilla
Sometimes a block of code can fail for reasons that require different handling, but which throw very similar errors (i.e. with the same...
Read more >Definitions and ODR (One Definition Rule) - cppreference.com
In C++, the source-code tokens used in declarations of the same type must be the same as described above: if one .cpp file...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Apologies for necro-ing this, but I figured it was more polite to comment here than open a new issue.
I agree with what you’ve said - that it’s good to avoid having a project where the same name is used twice - but I’ve just hit an issue where I’m generating a schema based on the AWS JS SDK, and I have one type that relies on
NetworkConfiguration
by proxy viaCreateServiceRequest
in@aws-sdk/client-ecs
and another that refers toNetworkConfiguration
directly in@aws-sdk/client-cloudwatch-events
.Because each AWS service is a beast unto itself these are similar but still different types.
Renaming the type by importing as
import { NetworkConfiguration as CWNetworkConfiguration } from '@aws-sdk/client-cloudwatch-events'
did not work - it still complained about the duplicate type.Creating an entirely new type by aliasing it, eg:
type CWNetworkConfiguration = NetworkConfiguration
, did not work.Creating a new type that TS would infer as being different (even though it isn’t) via
type CWNetworkConfiguration = Partial<NetworkConfiguration>
then gave me an error about a type that it refers to,AwsVpcConfiguration
, and at that point I gave up and copy/pasted the type from AWS’s typedefs and renamed each individual item by hand.Is there another workaround?
This simple case seems a legitimate and reasonable construction of a program that triggers this error:
ChildA.ts
ChildB.ts
Parent.ts
I’ve been looking at the code to figure a valid discriminant and not sure there is an easy path here. Perhaps using the id instead of the name when there is a conflict? Though ideally, it’d be good to have the file path in the mix, to allow human comprehension.
Wondering if others have figured clever work arounds (renaming is not an option for my use-case).