Skip to content
New issue

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

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

Already on GitHub? # to your account

[QUESTION] How to create typed function with RegExp or MagicRegExp input? (TypeScript) #392

Closed
kravetsone opened this issue Apr 16, 2024 · 2 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@kravetsone
Copy link

📚 Is your documentation request related to a problem?

How to create typed function with RegExp or MagicRegExp input?

I write something like

function getMatchedResult<R extends MagicRegExp<any, any, any, any> | RegExp>(regexp: R, str: string) {
	return str.match(regexp);
}

it doesn't matter Magic Regexp or just Regexp you place the function ReturnType is RegExpMatchArray | null

And is it possible not to use magic regexp as a dependency but to support it? (for example maybe implement compability types)

I am the maintainer of the Telegram Bot API framework - GramIO
And if i add support magic-regexp it would be amazing

image

I need to allow magic (with type-safety) and not regexp

🔍 Where should you find it?

No response

ℹ️ Additional context

No response

@kravetsone kravetsone added the documentation Improvements or additions to documentation label Apr 16, 2024
@danielroe
Copy link
Member

danielroe commented Mar 17, 2025

You need to copy the types used here:

https://github.com/danielroe/magic-regexp/blob/fda64609021c5f1bc72610a4a7d0e588a7c3ce87/src/index.ts#L54-L59

For example:

import type { Flag, MagicRegExp, MagicRegExpMatchArray } from 'magic-regexp'

function getMatchedResult<R extends MagicRegExp<string, string, (string | undefined)[], Exclude<Flag, 'g'>>>(regexp: R, str: string): MagicRegExpMatchArray<R> | null
function getMatchedResult<R extends MagicRegExp<string, string, (string | undefined)[], 'g'>>(regexp: R, str: string): string[] | null
function getMatchedResult(regexp: RegExp, str: string): RegExpMatchArray | null
function getMatchedResult<R extends MagicRegExp<string, string, (string | undefined)[], string> | RegExp>(regexp: R, str: string) {
  return str.match(regexp) as any
}

@kravetsone
Copy link
Author

You need to copy the types used here:

https://github.com/danielroe/magic-regexp/blob/fda64609021c5f1bc72610a4a7d0e588a7c3ce87/src/index.ts#L54-L59

For example:

import type { Flag, MagicRegExp, MagicRegExpMatchArray } from 'magic-regexp'

function getMatchedResult<R extends MagicRegExp<string, string, (string | undefined)[], Exclude<Flag, 'g'>>>(regexp: R, str: string): MagicRegExpMatchArray | null
function getMatchedResult<R extends MagicRegExp<string, string, (string | undefined)[], 'g'>>(regexp: R, str: string): string[] | null
function getMatchedResult(regexp: RegExp, str: string): RegExpMatchArray | null
function getMatchedResult<R extends MagicRegExp<string, string, (string | undefined)[], string> | RegExp>(regexp: R, str: string) {
return str.match(regexp) as any
}

Thats fine! But if force users to download package
I want to make it optional and this is so hard... Because this project uses Symbols i cant implement MagicRegExpLike...

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants