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

[Improvement] Check missing properties when type is copied #111

Closed
borissuska opened this issue Jul 5, 2022 · 2 comments
Closed

[Improvement] Check missing properties when type is copied #111

borissuska opened this issue Jul 5, 2022 · 2 comments
Labels
enhancement New feature or request typescript Related to Typescript

Comments

@borissuska
Copy link

Hello, it's me again. I was curious how issue #108 should happen. Is there a way how to use typescript to check if all properties are used?

I was looking at operation-node-transformer.ts source code mainly. There is used class like this:

export interface CreateSchemaNode extends OperationNode {
  readonly kind: 'CreateSchemaNode'
  readonly schema: IdentifierNode
  readonly ifNotExists?: boolean
}

and OperationNodeTransformer has copy method like this:

protected transformCreateSchema(node: CreateSchemaNode): CreateSchemaNode {
    return {
      kind: 'CreateSchemaNode',
      schema: this.transformNode(node.schema),
      ifNotExists: node.ifNotExists,
    }
  }

if I remove ifNotExists: node.ifNotExists, line no typescript error occurs because ifNotExists property is optional. I was looking for some utility type like build in Required. Sadly Required is not compatible with original T type, in this case it requires node.ifNotExists not to be possibly undefined. Long story short, after all I found out this utility type:

type AllProps<T> =  T & { [P in keyof T]-?: unknown; };

Now when I use the AllProps type like this

protected transformCreateSchema(node: CreateSchemaNode): AllProps<CreateSchemaNode> {
    return {
      kind: 'CreateSchemaNode',
      schema: this.transformNode(node.schema),
      // ifNotExists: node.ifNotExists,
    }
  }

Typescript shows error Property 'ifNotExists' is missing in type '{ kind: "CreateSchemaNode"; schema: IdentifierNode; }' but required in type '{ readonly kind: unknown; readonly schema: unknown; readonly ifNotExists: unknown; }' now. It's not very accurate, because type of all properties is unknown. In case of ifNotExists is initialised with string, Typescript shows Type 'string' is not assignable to type 'boolean | undefined'. error. Which is good enough because Typescript shows missing property and wrong property type errors. The only problem is, it changes OperationNodeTransformer interface, transform methods return type is AllProps<...Node> instead of ...Node, even types are compatible.

I can add it to the library if you agree.

@koskimas
Copy link
Member

koskimas commented Jul 6, 2022

Great idea! Feel free to implement and I'll merge. Thanks for looking into this 👍

By the way, I added a TEST_TRANSFORMER=1 npm test run to CI. But that only catches the forgotten stuff if there's a test for the feature.

@koskimas
Copy link
Member

koskimas commented Jul 9, 2022

PR merged. Thank you 👍

@koskimas koskimas closed this as completed Jul 9, 2022
@igalklebanov igalklebanov added typescript Related to Typescript enhancement New feature or request labels Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request typescript Related to Typescript
Projects
None yet
Development

No branches or pull requests

3 participants