ts-json-schema-generator
ts-json-schema-generator copied to clipboard
[REG 1.0->1.1] Omitted optional functions are throwing `Error: Unknown type "function"`
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:
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.
Confirming this issue is still present in v1.2.0
@loopingz Bumping this one to see if this issue can be fixed since the #1182 fix did not resolve it. Thanks
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.
So, when can we solve it?
@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 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
@loopingz Issues is still present and looking into this issue would be highly appreciated 🙏
:rocket: Issue was released in v2.0.0
:rocket: