ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

[REG 1.0->1.1] Omitted optional functions are throwing `Error: Unknown type "function"`

Open ChromeQ opened this issue 2 years ago • 7 comments

I have a simple User interface and try to create the schema. The User can have some functions, which won't work so there is a Jsonify type that will strip out the functions, this is adequate for my needs. Here is a simplified sample of a working TS file.

interface User {
    firstName: string;
    lastName: string;
    age: number;
    getFullName: () => string;
}

type Jsonify<T> = Pick<
    T,
    Exclude<
        {
            [K in keyof T]: NonNullable<T[K]> extends (...args: any[]) => any ? never : K;
        }[keyof T],
        undefined
    >
>;

export type JSONUser = Jsonify<User>;

Now generate the schema with:

npx ts-json-schema-generator -p ./test.ts -t JSONUser -e none --no-type-check -o ./test-schema.json

Outputs the schema as expected. All good 👍

However, now replace getFullName with getFullName? making it optional. Run the command again and we receive:

Error: Unknown type "function"

But the VSCode TS hint shows the JSONUser does NOT contain the function: image

This is a regression in v1.1.0, as v1.0.0 works. I have narrowed it down to the Typescript dependency which was upgraded in v1.1.0 https://github.com/vega/ts-json-schema-generator/pull/1388/files TS v4.7.4 is fine, v4.8+ is not.

I have also tried many variations of the Jsonify type above, with all of the examples on SO: https://stackoverflow.com/questions/55479658/how-to-create-a-type-excluding-instance-methods-from-a-class-in-typescript But they are all failing in the same way.

ChromeQ avatar Oct 27 '22 05:10 ChromeQ

Confirming this issue is still present in v1.2.0

ChromeQ avatar Dec 26 '22 01:12 ChromeQ

@loopingz Bumping this one to see if this issue can be fixed since the #1182 fix did not resolve it. Thanks

ChromeQ avatar Apr 07 '23 03:04 ChromeQ

I can have a look, I personally use my own formatters: https://github.com/loopingz/webda.io/blob/main/packages/shell/src/code/compiler.ts#L154-243

So constructors are also inclined to this type of issue.

loopingz avatar Apr 07 '23 04:04 loopingz

So, when can we solve it?

anymouschina avatar Apr 11 '23 02:04 anymouschina

@anymouschina maybe you can contribute a patch if you have time.

On my side, I'll have a look when I can, this is not a top priority for me, but will try this week, as I need to check the typescript 5 migration

loopingz avatar Apr 11 '23 02:04 loopingz

@loopingz I have resolved my problem by referring to this link : https://github.com/loopingz/webda.io/blob/main/packages/shell/src/code/compiler.ts#L154-243

anymouschina avatar Apr 11 '23 06:04 anymouschina

@loopingz Issues is still present and looking into this issue would be highly appreciated 🙏

andrekovac avatar Jun 17 '23 11:06 andrekovac

:rocket: Issue was released in v2.0.0 :rocket:

github-actions[bot] avatar Apr 13 '24 03:04 github-actions[bot]