Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some template literals types will break the generator #157

Open
SamuelCharpentier opened this issue Oct 19, 2021 · 2 comments
Open

Some template literals types will break the generator #157

SamuelCharpentier opened this issue Oct 19, 2021 · 2 comments
Labels
bug Something isn't working enhancement New feature or request pr requested

Comments

@SamuelCharpentier
Copy link

SamuelCharpentier commented Oct 19, 2021

As soon as you use a "wildcard" in a template literal string like `${string}`, the generator breaks down.

For example, if you have an email type like this:

type Email = `${string}@${string}.${string}`

the generated typeguard looks like this:

import { Email } from "./Email";

export function Email(obj: any, _argumentName?: string): obj is Email {
    return (
        typeof obj === "`${string}@${string}.${string}`"
    )
}

and that don't work as the typeof obj would be "string".

@SamuelCharpentier SamuelCharpentier changed the title Some template literals types don't work Some template literals types will break the generator Oct 19, 2021
@rhys-vdw rhys-vdw added the enhancement New feature or request label Oct 20, 2021
@rhys-vdw
Copy link
Owner

Oh yeah, that's a non-trivial fix. This project was created before template literal types were a thing. We'd need some sort of regex validator to do it properly, but for now falling back to a regular string check and logging an error would be best.

I'll accept a PR that provides either fix.

@elliot-huffman
Copy link
Collaborator

After researching this, I don't think a complex validator is necessary. All template strings are always string in the typeof key word. Even if the values are null, undefined, numbers, objects, etc. in the template string itself.
I think it is safe to set the logic to the following:
"If a template string is detected, validate the stored type is a string."

I think validating the content of the string would be where the regex is necessary, the object type (contain of the string content) will always be string though.

@elliot-huffman elliot-huffman added the bug Something isn't working label Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request pr requested
Projects
None yet
Development

No branches or pull requests

3 participants