TizzySaurus
TizzySaurus
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
Not even that. Just use a discriminated union, like you would with raw TS.
// raw TS
type t = {agree: true, clauses: "first" | "second"} | {agree: false, clauses?: "first" | "second"

// arktype
const t = type({agree: true, clauses: "'first' | 'second'"}).or({agree: false, clauses?: "'first' | 'second'"})
// raw TS
type t = {agree: true, clauses: "first" | "second"} | {agree: false, clauses?: "first" | "second"

// arktype
const t = type({agree: true, clauses: "'first' | 'second'"}).or({agree: false, clauses?: "'first' | 'second'"})
8 replies
Aarktype
Created by errata on 9/10/2024 in #questions
template literal/backtick string types e.g. type T = `type-${string}`
Although the inferred type would just be string
14 replies
Aarktype
Created by errata on 9/10/2024 in #questions
template literal/backtick string types e.g. type T = `type-${string}`
const UserId = type("string").narrow(s => s.startsWith("user-"))
const UserId = type("string").narrow(s => s.startsWith("user-"))
or whatever
14 replies
Aarktype
Created by errata on 9/10/2024 in #questions
template literal/backtick string types e.g. type T = `type-${string}`
You'd need to use a morph/narrow for now (probably a narrow)
14 replies
Aarktype
Created by errata on 9/10/2024 in #questions
template literal/backtick string types e.g. type T = `type-${string}`
Ig this is what you're referring to: https://github.com/arktypeio/arktype/issues/491
14 replies
Aarktype
Created by SynthLuvr on 9/4/2024 in #questions
Record with number as key?
No description
6 replies
Aarktype
Created by SynthLuvr on 9/4/2024 in #questions
Record with number as key?
It's not possible to have numbers as a key in JS
6 replies
Aarktype
Created by PIat on 8/30/2024 in #questions
Default number
Where t in this case is your queryType
133 replies
Aarktype
Created by PIat on 8/30/2024 in #questions
Default number
It's not clear to me what exactly you're after here, but maybe t["infer"] or t["tOut"] is what you're after?
133 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
@PIat
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
(That json being formType.json)
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
No description
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
Sort of. You could look at the internal schema structure
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
You just need a way of determining what keepAsString should be, which from the sounds of it you know so isn't an issue
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
@PIat To finally answer your question, this seems to work:
import { type } from "arktype"

const formType = type({
intent: "'profile'",
name: "string>0",
surname: "string>4",
agree: "boolean"
}).or({
intent: "'private'",
password: "string>0"
})

const keepAsString = ["intent", "name", "surname", "password"]

const parseNonStringFieldsToJson = type("object", "=>", obj =>
Object.fromEntries(
Object.entries(obj).map(([k, v]) => [
k,
keepAsString.includes(k) ? v : JSON.parse(v)
])
)
)

const parseFormData = parseNonStringFieldsToJson.pipe(formType)
const input = {
intent: "profile",
name: "John",
surname: "Doe",
agree: "true"
}

const result = parseFormData.assert(input)
console.log(result)
import { type } from "arktype"

const formType = type({
intent: "'profile'",
name: "string>0",
surname: "string>4",
agree: "boolean"
}).or({
intent: "'private'",
password: "string>0"
})

const keepAsString = ["intent", "name", "surname", "password"]

const parseNonStringFieldsToJson = type("object", "=>", obj =>
Object.fromEntries(
Object.entries(obj).map(([k, v]) => [
k,
keepAsString.includes(k) ? v : JSON.parse(v)
])
)
)

const parseFormData = parseNonStringFieldsToJson.pipe(formType)
const input = {
intent: "profile",
name: "John",
surname: "Doe",
agree: "true"
}

const result = parseFormData.assert(input)
console.log(result)
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
As in, I'm too busy to work on it atm anyway
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
Yeah, my time is better spent elsewhere atm anyway tbf
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
I'm just waiting on https://github.com/arktypeio/arktype/issues/1033 (friendly nudge 😉)
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
Yeah, it would've happened anyway when I continued working on https://github.com/arktypeio/arktype/issues/729 😅
187 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
Accept number in string type
(Sorry @PIat, I totally took over this channel 😅)
187 replies