Skip to content

Feature request: infer literal value types for captures #19

@Raiondesu

Description

@Raiondesu

Currently, when parsing regexes with captures, their types are inferred as either string or string | undefined depending on the capture definition.

However, there are many simple use-cases that would benefit from analyzing captures more literally and having stricter typing for their values:

const fruit = typedRegExp('(banana|apple|pear|orange)');

const banana = fruit.exec('banana')![1];
// Currently typed as:
// string

// Could be typed as:
// 'banana' | 'apple' | 'pear' | 'orange'

// But since we know the type of the string, it could be typed as just:
// 'banana' | 'apple' | 'pear' | 'orange' & 'banana' -> just 'banana'
// This is, however, unrealistic for more complex patterns...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions