Skip to content

String#matchAll is too restrictive in its parameter typeΒ #55843

@bbrk24

Description

@bbrk24

πŸ”Ž Search Terms

matchAll

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about RegExps and matchAll

⏯ Playground Link

https://www.typescriptlang.org/play?target=7#code/MYewdgzgLgBAtgTwMIFdojgeQEYCsCmwsAvDAN4CwAUDLTAFQDaAygnNiADYB0cAhlGAALAIKdOAXQAU0AE4BLMAHMAXDDmKlASjUBxfGHyyBIWQB4ASviUBRAB4AHe4RGzjCAHzlqdGAF9qAKpqUEgufG5OECUpRm54gHIAMxAQBN4BYTFOKURUdCw8QigtCS0AbmogA

πŸ’» Code

const myCustomObject = {
    *[Symbol.matchAll](string: string): Generator<RegExpExecArray> {
    }
}

console.log([...'foo'.matchAll(myCustomObject)]);

πŸ™ Actual behavior

TS rejects this code, because myCustomObject is not an instance of RegExp.

πŸ™‚ Expected behavior

Any value matching the following type should be accepted, including myCustomObject. The other parts of the RegExp object are not used.

type MatchAllRegExpLike = {
  [Symbol.matchAll](string: string): IterableIterator<RegExpExecArray>;
  // If the object has a [Symbol.match] property that is truthy, its flags are checked. However, neither
  // must be present, and even if it is present, [Symbol.match] doesn't have to be a function.
  readonly [Symbol.match]?: any;
  readonly flags?: string;
};

Additional information about the issue

This is distinct from #47310. That issue is about string-to-regexp conversion; this is about regexp-like objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions