arktypeio / arktype

TypeScript's 1:1 validator, optimized from editor to runtime
https://arktype.io/
MIT License
3.96k stars 62 forks source link

Extending type #918

Closed collmomo closed 8 months ago

collmomo commented 8 months ago

Request a feature

Sorry if this was already asked, i didn't find anything on the issues list. Simply said, having the ability to extend a type definition in the scope section, instead of having to repeat the same variable requirements.

🤷 Motivation

Repeating my variable definitions in the scope definition while I could simply extend an "abstract type"

Why should we prioritize solving it? no need to prioritize, it just aligns with the DRY principle.

💡 Solution

How do you think we should solve the problem? scope({base:{name:"string"}, {'child<base':{newProperty : "string"}})

Why do you think this is the best solution? it's easy, I'm just not sure on the symbol to use

Did you consider any alternatives? I checked unions and intersections but there's not many documentation on these features

ssalbdivad commented 8 months ago

An intersection is the easiest way to achieve this. You can either inline it with a tuple expression:

scope({
    base: {name: "string"},
    child: ["base", "&", {newProperty: "string"}]
})

Or you could define the additional props separately and intersect them like this:

scope({
    base: {name: "string"},
    extras: {newProperty: "string"},
    child: "base&extras"
})
collmomo commented 8 months ago

thanks, I opted for the first choice, just fyi, I'm defining all my api calls within a single scope variable that I exported and I have multiple class entities that are hydrated by those api calls, so im typing these response.data with the infer property by importing and checking for runtime errors.

Great library btw, took me a while to find the one I like, I tried typebox (error message not working), tried suretype, but there was missing features I wanted and some functions were not working (ensure), zod was not for my liking, I wanted runtypes but it's not performing well, and yours checked everything plus eventually performance wise with 2.0 so thanks!

ssalbdivad commented 8 months ago

That's great to hear! I'm actually hoping to release a new dev build for 2.0 today. Feel free to join the Discord where I'll post it if you haven't already 🙂

https://arktype.io/discord