#Help with recursive type

19 messages · Page 1 of 1 (latest)

hollow knoll
#

I'm trying to make a type that uses recursion. I'm not sure the rules around recursion, I found some Stackoverflow posts on the topic, but they are all several years old and from what I understand rules around recursion have changed since then. I have created a playground, linked below, which highlights my current code, the errors it has, and what my goal is for this type. Thanks for any help, even if its just to tell me its impossible.

Playground

keen palmBOT
#

@hollow knoll Here's a shortened URL of your playground link! You can remove the full link from your message.

surprised_pika#0

Preview:ts type letter = | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" type digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 type validNamespaceCharacter = | letter | digit | "_" | "." | "-" ...

charred pawn
#

Syntax aside, that's simply impossible.

#

There are infinite amount of valid namespaces, what you are trying to do is basically "generate all prime numbers"

#

The best you can do is "check if a given number is prime"

hollow knoll
#

What I was trying to do was "check if a given number is prime", as you call it. I just want a type that will give an error if a variable assigned to that type isn't a valid namespace. I don't want to generate every namespace, and if that is what my code is doing it's not by intention.

charred pawn
#

Yeah checking in the form of const x: Namespace = '...' isn't possible, because Namespace has to contain all possible namespaces in order to perform that check.

#

However you can check in the form of const x = namespace('...') using a validator pattern.

#

But validator pattern can be worked around, so it really depends on how you are trying to use it.

hollow knoll
#

I'm fine with it being able to be worked around, but I need it to be completely removed when the code is transpiled, so you would just get const x = '...'. Is that possible using a validator pattern?

charred pawn
#

No, validator has to use a function call.

#

Why do you have to remove the function call? The function call itself could just be the equivalent of x => x.

hollow knoll
#

I guess "have to" is a strong word but the transpiled file is sent to another program which is very flaky based on input. I suppose it should be fine however, on second thought

#

Thanks for your help, appreciate it

#

!resolved

charred pawn
#

For completeness, here's a very simple example of validator pattern:

keen palmBOT
#
type Validate<T> = T extends `${'x'}${string}` ? T : never

const validate = <T extends string>(x: Validate<T>) => x

validate('')
//       ^^
// Argument of type 'string' is not assignable to parameter of type 'never'.
validate('a')
//       ^^^
// Argument of type 'string' is not assignable to parameter of type 'never'.
validate('x')
validate('x123')
charred pawn
#

This example validates that the string must start with x.

#

You can encode any logic into Validate<T>, and validate is just x => x after compiling.