A
arktype4mo ago
errata

alternative to nesting quotes in quotes for strings?

const Target = type("'BODY' | 'FACE' | 'ARMS'")

const Entity = type({
targets: Target.array()
})
const EntityDefn = generic(['T', Entity])('T')
const Creature = EntityDefn({
targets: ["'BODY'", "'FACE'"]
})
const Target = type("'BODY' | 'FACE' | 'ARMS'")

const Entity = type({
targets: Target.array()
})
const EntityDefn = generic(['T', Entity])('T')
const Creature = EntityDefn({
targets: ["'BODY'", "'FACE'"]
})
Needing to double + single quote everything feels really fiddly and clumsy, like, I feel like I don't my coworkers to see this code lest they judge me. Is there a cleaner, alternative syntax/api?
14 Replies
ssalbdivad
ssalbdivad4mo ago
It's better with syntax highlighting, download the extension. Also, in the docs we do have, I m ention type.enumerated so you can use that if you prefer
errata
errataOP4mo ago
@ArkDavid ok i figured out the type.enumerated for intial definition, but I'm still struggling with a bit, see below:
const Target = type.enumerated('BODY', 'FACE' , 'ARMS')

const Entity = type({
targets: Target.array()
})
const EntityDefn = generic(['T', Entity])('T')
const Creature = EntityDefn({
// targets: type.enumerated('BODY', 'FACE'), // nope
// targets: Target.array().pick('BODY', 'FACE') // nope
// targets: Target.array().from(['BODY', 'FACE']) // nor this. what goes here ??
targets: [type.unit('BODY'), type.unit('FACE')], // ahh!
})
const Target = type.enumerated('BODY', 'FACE' , 'ARMS')

const Entity = type({
targets: Target.array()
})
const EntityDefn = generic(['T', Entity])('T')
const Creature = EntityDefn({
// targets: type.enumerated('BODY', 'FACE'), // nope
// targets: Target.array().pick('BODY', 'FACE') // nope
// targets: Target.array().from(['BODY', 'FACE']) // nor this. what goes here ??
targets: [type.unit('BODY'), type.unit('FACE')], // ahh!
})
I don't use vscode
ssalbdivad
ssalbdivad4mo ago
Ahh RIP
errata
errataOP4mo ago
I also worry about ability to use refactoring tools with stuff being strings sure, the compiler will complain but I'd prefer if I didn't have to think about writing an additional regex for find/replace after every refactor ahh got it. targets: [type.unit('BODY'), type.unit('FACE')], might be good to have some shorthand aliases for type.enumerated() and type.kind(), they're a bit wordy
ssalbdivad
ssalbdivad4mo ago
Then make them lol
errata
errataOP4mo ago
yeah of course I can add them myself
ssalbdivad
ssalbdivad4mo ago
You may find this hard to believe but I have put orders of magnitude more thought into every decision related to this library than all these random suggestions so if something is a particular way, it's probably not because I didn't think of an alternative name for the API
errata
errataOP4mo ago
oh I believe it
ssalbdivad
ssalbdivad4mo ago
Then please, I would find it a lot easier to help out where I can if you stopped suggesting changes based on the first thoughts you have using the library
errata
errataOP4mo ago
ok Though to be clear, the previous suggestion was not a request to rename enumerated or unit, these are good names and are plenty clear now I see how they are used, my suggestion was to add some additional short aliases (like $) in the interest of making the fluent api feel comparable to the nice and terse string api. Though now i say it, I'm sure you have considered this too. I will limit the suggestions going forward. My apologies, and thank you for all your help and patience thus far. I just get excited about new tools
ssalbdivad
ssalbdivad4mo ago
Thank you, I appreciate that! I don't anticipate most people using literals so frequently as I think the types you are trying to build are a bit unique, but I can see how it could get a bit verbose. Feel free to destructure/rename enumerated and/or unit from type. As far as refactoring, one general strategy is to create an object, then use a narrowed Object.keys which you can pass to type.enumerated (or anywhere really, it can just be a good native alternative to an enum in TS) Also I'm looking for someone who uses neovim to add support for ArkDark's textmate scopes which I think makes a huge difference for DX so let me know if you're interested in tackling that
errata
errataOP4mo ago
let me know if you're interested in tackling that
ah that would be neat, but no bandwidth for it currently
As far as refactoring, one general strategy is to create an object, then use a narrowed Object.keys which you can pass to type.enumerated (or anywhere really, it can just be a good native alternative to an enum in TS)
This is basically what TS enum {FOO = 'FOO' } compile to, no?
ssalbdivad
ssalbdivad4mo ago
Not exactly. A lot of people wouldn't recommend enums in modern TS because they're a bit opaque externally
errata
errataOP4mo ago
that's definitely true for numeric enums, you end up passing around a lot of mysterious 0s and 1s and 2s but I struggle to follow that for the newer stringy enums, they're just strings on an object but yeah ok good to know
Want results from more Discord servers?
Add your server