Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disambiguate type with same name in different files #113

Closed
marcoqu opened this issue Jun 3, 2019 · 13 comments
Closed

Disambiguate type with same name in different files #113

marcoqu opened this issue Jun 3, 2019 · 13 comments

Comments

@marcoqu
Copy link
Contributor

marcoqu commented Jun 3, 2019

In my use case I need to disambiguate what specific type I want to convert, and in a given project there might be more than one type with the same name, in different files.

As in my extension the user activates ts-json-schema-generator with reference to a specific file, the ability to specify a file when searching for types would greatly help. At the moment I'm implementing the ability in a fork, but I would love to depend on the original library.

@domoritz
Copy link
Member

domoritz commented Jun 3, 2019

If I remember correctly, this tool should throw a warning when there are multiple types with the same name. Disambiguation should actually not be required if only one type is actually needed to produce the output.

@kayahr
Copy link
Contributor

kayahr commented Jun 23, 2019

Can be reproduced by generating the schema for MyObject in this example:

// 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;
};

Will produce the error message Error: Type "Field" has multiple definitions.

@kayahr
Copy link
Contributor

kayahr commented Jun 23, 2019

Oh, I'm sorry, I think I misunderstood this issue. This is more about having multiple types with the same name and telling the generator for WHICH type the schema should be generated. MY problem is different so the reproduction above does not belong here. I'm opening a new issue (Created #143)

@marcoqu
Copy link
Contributor Author

marcoqu commented Sep 10, 2019

If I remember correctly, this tool should throw a warning when there are multiple types with the same name. Disambiguation should actually not be required if only one type is actually needed to produce the output.

Yes, my problem is that there are some cases in which some types have the same name (sometimes the names are duplicated by some imported types, from external libraries), and I need to tell the generator for which type the schema should be generated.

The example by @kayahr is correct, but I need to tell the generator that i need the schema for Field in the file test.ts.

@domoritz
Copy link
Member

domoritz commented Sep 20, 2019

YousefED/typescript-json-schema#151 introduced a uniqueNames options to YousefED/typescript-json-schema. This may be a viable solution.

@marcoqu
Copy link
Contributor Author

marcoqu commented Sep 20, 2019

I believe that is a different issue. What I'm looking for is a way to specify for which type I want a schema to be generated. The issue you are referencing seems to be about how to export multiple types with the same name, if I understand correctly..

@domoritz
Copy link
Member

domoritz commented Sep 20, 2019

I see. We don't really need an option then because TypeScript already knows what your you're referring to, right? To me, there is no ambiguity to resolve, but there is a bug.

@marcoqu
Copy link
Contributor Author

marcoqu commented Sep 20, 2019

We don't really need an option then because TypeScript already knows what your you're referring to, right?

I may be missing something, but I don't know how I can tell the generator that I want the schema for MyType in File.ts and not MyType in File2.ts. When both File.ts and File2.ts are part of my typescript project.

@domoritz
Copy link
Member

Ohh, so this is about the root type only. I see. Ideally, this would just work if you provide the file with -p. Do you have a small example?

@marcoqu
Copy link
Contributor Author

marcoqu commented Sep 21, 2019

Right, except (if I understand correctly) using -p specifies the path to the TypeScript source file. If the file is part of a project, references to other file types will not be resolved.
I'll try to make a small test case.

@domoritz
Copy link
Member

You can use both -f and -p. I'm probably still missing something.

@marcoqu
Copy link
Contributor Author

marcoqu commented Sep 23, 2019

Hi, I checked.
Apparently the issue is that, if you use -f and -p, the files property of tsconfig.json gets ignored, and if your target file the program compilation depends on some file that your required through your tsconfig.json, the program creation fails.

tsconfig.json

{
    "files": [
        "custom.d.ts",
        "main.ts"
    ]
}

main.ts

import { ScriptData as SD } from "./scriptData";
export type ScriptData = { scriptDataInMain?: boolean; }

custom.d.ts

declare module '*.csv' {
    let csv: string;
    export default csv;
}

scriptData.ts

import data from "./data.csv";
export type ScriptData = { scriptDataInScriptData?: boolean; }

npx ts-json-schema-generator -t ScriptData -f .\tsconfig.json

{
  "$ref": "#/definitions/ScriptData",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "ScriptData": {
      "additionalProperties": false,
      "properties": {
        "scriptDataInMain": {
          "type": "boolean"
        }
      },
      "type": "object"
    }
  }
}

npx ts-json-schema-generator -t ScriptData -f .\tsconfig.json -p .\scriptData.ts

scriptData.ts(1,18): error TS2307: Cannot find module './data.csv'

@domoritz
Copy link
Member

I don't plan to implement this feature and therefore will close this issue. If anyone needs it, I would be more than happy to review a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants