ts-json-schema-generator
ts-json-schema-generator copied to clipboard
Better promise supports with `unwrap` property.
A while ago I submitted this, but it certainly don't work for custom promises or classes that extends promises... A simple example of that is Prisma itself that uses a custom Promise type to add better intellisense.
function test() {
return prisma.user.findFirst();
}
// this don't work because we cannot infer `Prisma.Prisma__UserClient` as a promise type.
export const user = test();
*****
async function test() {
return prisma.user.findFirst();
}
// this works because the async function transforms the `Prisma.Prisma__UserClient` into a `Promise` type.
export const user = test();
Which is very hard to debug and find out the problem, as no error at compile time is thrown.
To solve this and other problems, what about adding a unwrap property which would do exactly what it says:
{
"unwrap": ["Promise", "ExampleGenericType"] // defaults to only Promise
}
type ExampleGenericType<A,B,C> = ... /* multiple generics */
const test1: Promise<number>
// would result into number json array type.
const test2: ExampleGenericType<number, string, boolean>
// would result in a [number, string, boolean] tuple json array type.
const test3: ExampleGenericType<number>
// would result in a number json array type.
Of course this is a more open approach and you may not want it, in that case, what about just a promiseTypes: string[] property to add more promise types?