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: add support for multiple interface extensions #68

Merged
merged 2 commits into from Feb 3, 2022

Conversation

tvillaren
Copy link
Collaborator

@tvillaren tvillaren commented Feb 2, 2022

Why

As I had similar needs as the one mentioned in #67, i.e. to be able to support multiple interface extends, I decided to give it a go.

Input

With the following definition

// typescript type or interface causing the output
export interface a {
    type: string
}

export interface b {
    id: string
}

export interface c extends a, b {
    value: number
}

or

export interface c extends a extends b {
    value: number
}

Output

I don't think merging the Object Literals as described in the original ticket makes sense (and it's probably more difficult to implement 😉 ) so I want with a simple extend referencing the schema's shape property.

// Expected Zod schemas
import { z } from 'zod'

export const aSchema = z.object({
    type: z.string(),
})

export const bSchema = z.object({
    id: z.string(),
})

export const cSchema = aSchema.extend(bSchema.shape).extend({
    value: z.number(),
})

Note

I'm not sure of the semantic difference between using the "extend chain" (c extends a extends b) and the "extend types" (c extends a,b) so implemented them the same way for now. To be discussed maybe.

@codecov-commenter
Copy link

Codecov Report

Merging #68 (b8f094b) into main (e5e4961) will decrease coverage by 1.39%.
The diff coverage is 95.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #68      +/-   ##
==========================================
- Coverage   98.49%   97.09%   -1.40%     
==========================================
  Files           8       11       +3     
  Lines         332      482     +150     
  Branches      102      148      +46     
==========================================
+ Hits          327      468     +141     
- Misses          5       11       +6     
- Partials        0        3       +3     
Impacted Files Coverage Δ
src/core/generateZodSchema.ts 95.23% <92.40%> (-1.97%) ⬇️
src/utils/resolveDefaultProperties.ts 95.23% <95.23%> (ø)
src/utils/resolveModules.ts 97.87% <97.87%> (ø)
src/core/generate.ts 100.00% <100.00%> (ø)
src/core/jsDocTags.ts 98.50% <100.00%> (-1.50%) ⬇️
src/core/validateGeneratedTypes.ts 97.50% <100.00%> (+0.13%) ⬆️
src/utils/isNotNull.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b5043eb...b8f094b. Read the comment docs.

@tvillaren
Copy link
Collaborator Author

I'm not sure about the codecov report as I didn't change the src/core/jsDocTags.ts nor src/core/validateGeneratedTypes.ts files.
And the reported uncovered lines are mostly (but one) from code paths I haven't added.

@tvillaren tvillaren marked this pull request as ready for review February 2, 2022 17:13
@fabien0102
Copy link
Owner

I'm not sure about the codecov report as I didn't change the src/core/jsDocTags.ts nor src/core/validateGeneratedTypes.ts files. And the reported uncovered lines are mostly (but one) from code paths I haven't added.

Please ignore it, this tool is sadly not reliable and is almost always failing… I need to remove it

Copy link
Owner

@fabien0102 fabien0102 left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for the contribution, really appreciated!

@fabien0102 fabien0102 merged commit e349c33 into fabien0102:main Feb 3, 2022
@tvillaren tvillaren deleted the feat-multi-extend branch February 3, 2022 16:10
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.

None yet

3 participants