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

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

Closed
ChromeQ opened this issue Oct 27, 2022 · 8 comments · Fixed by #1910
Closed
Labels
released This issue/pull request has been released.

Comments

@ChromeQ
Copy link

ChromeQ commented Oct 27, 2022

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
Copy link
Author

ChromeQ commented Dec 26, 2022

Confirming this issue is still present in v1.2.0

@ChromeQ
Copy link
Author

ChromeQ commented Apr 7, 2023

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

@loopingz
Copy link
Contributor

loopingz commented Apr 7, 2023

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.

@anymouschina
Copy link

So, when can we solve it?

@loopingz
Copy link
Contributor

loopingz commented Apr 11, 2023

@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

@anymouschina
Copy link

@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

@andrekovac
Copy link

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

Copy link

🚀 Issue was released in v2.0.0 🚀

@github-actions github-actions bot added released This issue/pull request has been released. and removed prerelease labels Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants