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

feat: Improved Promise handling to support packages like Prisma #1924

Merged
merged 24 commits into from
May 20, 2024

Conversation

arthurfiorette
Copy link
Collaborator

@arthurfiorette arthurfiorette commented Apr 16, 2024

closes #1923
closes kitajs/kitajs#280

This PR adds support to any Promise<T> | PromiseLike<T> type to be extracted into T. As promises cannot be transformed into any value and can only be used once resolved, this is a safe assumption to extract.

My test case at test/valid-data/promise-extensions/main.ts ensures every "thenable" type is correctly extracted.

export type PromiseAlias = Promise<A>;

export class PromiseClass extends Promise<A> {}

export interface PromiseInterface extends Promise<A> {}

export type LikeType = PromiseLike<A>;

export type PromiseOrAlias = Promise<A> | A;

export type LikeOrType = PromiseLike<A> | A;

export type AndPromise = Promise<A> & { a: string };

export type AndLikePromise = PromiseLike<A> & { a: string };

export class LikeClass implements PromiseLike<A> {
    then<TResult1 = A, TResult2 = never>(
        onfulfilled?: ((value: A) => TResult1 | PromiseLike<TResult1>) | null | undefined,
        onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined
    ): PromiseLike<TResult1 | TResult2> {
        return new Promise(() => {});
    }
}

export abstract class LikeAbstractClass implements PromiseLike<A> {
    abstract then<TResult1 = A, TResult2 = never>(
        onfulfilled?: ((value: A) => TResult1 | PromiseLike<TResult1>) | null | undefined,
        onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined
    );
}

export interface LikeInterface extends PromiseLike<A> {}

// Prisma has a base promise type just like this
export interface WithProperty extends Promise<A> {
    [Symbol.toStringTag]: "WithProperty";
}

export interface ThenableInterface {
    then<TResult1 = A, TResult2 = never>(
        onfulfilled?: ((value: A) => TResult1 | PromiseLike<TResult1>) | null | undefined,
        onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined
    ): PromiseLike<TResult1 | TResult2>;
}

export class ThenableClass {
    then<TResult1 = A, TResult2 = never>(
        onfulfilled?: ((value: A) => TResult1 | PromiseLike<TResult1>) | null | undefined,
        onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined
    ): PromiseLike<TResult1 | TResult2> {
        return new Promise(() => {});
    }
}

All of them results into the following json schema:

{
  "PromiseAlias ": {
    "$ref": "#/definitions/A"
  }
}

(with their respective type name)

My base comparison was to strictly follow what the native Awaited native type added by microsoft/TypeScript#45350.

It works without much hassle by using a internal ts.TypeChecker property. I also added https://www.npmjs.com/package/ts-expose-internals as a dev dependency

Version

Published prerelease version: v2.2.0-next.0

Changelog

🎉 This release contains work from new contributors! 🎉

Thanks for all your work!

❤️ Sampsa Kaskela (@SampsaKaskela)

❤️ Rama Krishna Ghanta (@ramaghanta)

❤️ Tim (@timothympace)

🚀 Enhancement

🐛 Bug Fix

🔩 Dependency Updates

Authors: 6

@domoritz domoritz changed the title Improved Promise handling to support packages like Prisma feat: Improved Promise handling to support packages like Prisma Apr 16, 2024
@arthurfiorette arthurfiorette marked this pull request as ready for review May 16, 2024 01:43
@@ -27149,6 +27149,8 @@
},
"SingleDefUnitChannel": {
"enum": [
"text",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow this type started getting out of order and breaking CI:

https://github.com/vega/ts-json-schema-generator/actions/runs/9105537200/job/25031273060#step:7:25

I changed its order so tests can pass, I'm also almost sure enum is order insensitive.

@arthurfiorette
Copy link
Collaborator Author

Hey @domoritz, it's ready for review :)

@domoritz
Copy link
Member

I was waiting for tests to pass. I'll check when I'm back from traveling.

@arthurfiorette
Copy link
Collaborator Author

I was waiting for tests to pass

me too 😭

@arthurfiorette
Copy link
Collaborator Author

arthurfiorette commented May 16, 2024

@domoritz done!

Codecov action failed but all missing coverage entries are from safety checks that should never hit in the current typescript version.

image

Copy link
Member

@domoritz domoritz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. Just a few comments.

src/NodeParser/FunctionNodeParser.ts Outdated Show resolved Hide resolved
if (typeSymbol.name === "Promise" || typeSymbol.name === "PromiseLike") {
// Promise without type resolves to Promise<any>
if (!node.typeArguments || node.typeArguments.length === 0) {
return new AnyType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this logic into the PromiseNodeParser?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually not, It might be a skill issue of mine but I couldn't get it to work without this statement.

When I remove it, this is the error thrown:

  ● valid-data-type › promise-extensions

    TypeError: Cannot read properties of undefined (reading 'getId')

      123 |
      124 |     // Check for simple type equality
    > 125 |     if (source.getId() === target.getId()) {
          |                ^
      126 |         return true;
      127 |     }
      128 |

      at getId (src/Utils/isAssignableTo.ts:125:16)
      at src/NodeParser/ConditionalTypeNodeParser.ts:44:77
      at predicate (src/Utils/narrowType.ts:59:12)
      at ConditionalTypeNodeParser.createType (src/NodeParser/ConditionalTypeNodeParser.ts:44:41)
      at ChainNodeParser.createType (src/ChainNodeParser.ts:35:54)
      at TypeAliasNodeParser.createType (src/NodeParser/TypeAliasNodeParser.ts:40:43)
      at AnnotatedNodeParser.createType (src/NodeParser/AnnotatedNodeParser.ts:34:47)
      at ExposeNodeParser.createType (src/ExposeNodeParser.ts:23:45)
      at CircularReferenceNodeParser.createType (src/CircularReferenceNodeParser.ts:24:43)
      at ChainNodeParser.createType (src/ChainNodeParser.ts:35:54)
      at TypeReferenceNodeParser.createType (src/NodeParser/TypeReferenceNodeParser.ts:78:37)
      at ChainNodeParser.createType (src/ChainNodeParser.ts:35:54)
      at PromiseNodeParser.createType (src/NodeParser/PromiseNodeParser.ts:71:47)
      at ChainNodeParser.createType (src/ChainNodeParser.ts:35:54)
      at TypeReferenceNodeParser.createType (src/NodeParser/TypeReferenceNodeParser.ts:78:37)
      at ChainNodeParser.createType (src/ChainNodeParser.ts:35:54)
      at createType (src/NodeParser/UnionNodeParser.ts:22:45)
          at Array.map (<anonymous>)
      at UnionNodeParser.map [as createType] (src/NodeParser/UnionNodeParser.ts:21:14)
      at ChainNodeParser.createType (src/ChainNodeParser.ts:35:54)
      at TypeAliasNodeParser.createType (src/NodeParser/TypeAliasNodeParser.ts:40:43)
      at AnnotatedNodeParser.createType (src/NodeParser/AnnotatedNodeParser.ts:34:47)
      at ExposeNodeParser.createType (src/ExposeNodeParser.ts:23:45)
      at CircularReferenceNodeParser.createType (src/CircularReferenceNodeParser.ts:24:43)
      at ChainNodeParser.createType (src/ChainNodeParser.ts:35:54)
      at TopRefNodeParser.createType (src/TopRefNodeParser.ts:14:47)
      at createType (src/SchemaGenerator.ts:30:36)
          at Array.map (<anonymous>)
      at SchemaGenerator.map [as createSchemaFromNodes] (src/SchemaGenerator.ts:29:37)
      at SchemaGenerator.createSchemaFromNodes [as createSchema] (src/SchemaGenerator.ts:25:21)
      at Object.createSchema (test/utils.ts:61:34)

test/config.test.ts Outdated Show resolved Hide resolved
Co-authored-by: Dominik Moritz <domoritz@gmail.com>
@arthurfiorette
Copy link
Collaborator Author

is there anything left?

@domoritz domoritz merged commit 183b426 into vega:next May 20, 2024
2 of 4 checks passed
@arthurfiorette arthurfiorette self-assigned this May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Force async routes Improved Promise handling to support packages like Prisma
2 participants