A
arktype•6mo ago
Luca Schultz

Is it possible to extract a "subtype"?

Hi, I'm looking into Arktype for the first time, so please forgive me if this is a noob question 😅 I wanna extract a "subtype"/"subschema" from a schema. I don't know how describe it better other than a code example:
const someScope = scope({
Endpoint: {
response: {
name: 'string',
age: 'number',
},
params: {
body: {
name: 'string',
age: 'number',
},
},
},
}).export()

const Endpoint = someScope.Endpoint

// Should be equivalent to type({ name: 'string', age: 'number' })
const Body = Endpoint.params.body
const someScope = scope({
Endpoint: {
response: {
name: 'string',
age: 'number',
},
params: {
body: {
name: 'string',
age: 'number',
},
},
},
}).export()

const Endpoint = someScope.Endpoint

// Should be equivalent to type({ name: 'string', age: 'number' })
const Body = Endpoint.params.body
8 Replies
Dimava
Dimava•6mo ago
btw, Why not export them in the scope?
Luca Schultz
Luca SchultzOP•6mo ago
Ah, I probably should have added that I don't control the scope. It is generated using https://github.com/astahmer/typed-openapi
GitHub
GitHub - astahmer/typed-openapi: Generate a headless Typescript API...
Generate a headless Typescript API client from an OpenAPI spec - astahmer/typed-openapi
PIat
PIat•6mo ago
You can use chained .get calls
const body = Endpoint.get('params').get('body')
const body = Endpoint.get('params').get('body')
Luca Schultz
Luca SchultzOP•6mo ago
@PIat that doesn't seem to be possible or am I doing it wrong?
No description
PIat
PIat•6mo ago
Strange! Which version are you on?
PIat
PIat•6mo ago
No description
No description
Luca Schultz
Luca SchultzOP•6mo ago
Ah, I upgraded to "2.0.0-beta.5" and now it works! Thank you!
PIat
PIat•6mo ago
Perfect!

Did you find this page helpful?