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

RegExp.prototype.exec() and test() parameters are optional #37211

Closed
newlukai opened this issue Mar 4, 2020 · 6 comments
Closed

RegExp.prototype.exec() and test() parameters are optional #37211

newlukai opened this issue Mar 4, 2020 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@newlukai
Copy link

newlukai commented Mar 4, 2020

TypeScript Version:
3.8.3

Search Terms:
RegExp

Code

import * as assert from 'assert';

let matches = /ab(c)/.exec('abc');
assert.ok(Array.isArray(matches));
assert.equal((matches as RegExpExecArray).length, 2);

matches = /ab(c)/.exec();
assert.equal(matches, null);

Expected behavior:
Compiles.

Actual behavior:
Doesn't compile due to an expected argument for exec in line 8. But the corresponding JavaScript code runs without error.

Playground Link:
http://www.typescriptlang.org/play/?ssl=8&ssc=29&pln=1&pc=1#code/JYWwDg9gTgLgBAKjgQwM4tagprOAzKCEOAcjW1hIG4BYAKHoBst4RkYBjACy3QF44AemQAjABQcAlIIB0WAB5YOYsiI4lJtOuRwwZEANZiAglCjIAnjOCpT5i2LaceqSZvo7YcgI4BXZIxijuzcvBhwAEpYAOYAovJg8Up2lpIyzAB20TBcADRwAEzuDHROofxCohLScorKxZ56WH4Bwc68+Rm+jIyaQA

Related Issues:
Did not find other bugs that looked similar.

@ilogico
Copy link

ilogico commented Mar 4, 2020

It runs without errors but it's certainly a mistake.

@IllusionMH
Copy link
Contributor

While it works in JS, JS also has no problems with null, 123 and {a: 123} (doesn't throw an error), how implicit undefined is better?

Just use .exec('undefined') for same effect but at least it won be confusing when /und/.exec() != null and /und/.test() === true.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 4, 2020
@newlukai
Copy link
Author

newlukai commented Mar 5, 2020

I proposed this because I had an issue when using an optional chain:
/abc/.exec(someObject.someSubObject?.someValue);
What TypeScript defines as "JS is improper about this" forces me to write
/abc/.exec((someObject.someSubObject?.someValue as string)).

I'm wondering if that's intended. I could live with it, but I think it's an aspect where TS feels more challenging.

@ilogico
Copy link

ilogico commented Mar 5, 2020

Like @IllusionMH suggested, you might as well write /abc/.exec(someObject.someSubObject?.someValue ?? 'undefined'), which is probably not what you meant, but it's equivalent and more explicit.
That code is wrong and TypeScript is warning you about it. You're using casts to eliminate the warning instead of admitting and correcting the mistake :)
You probably mean something like someObject.someSubObject?.someValue != null ? /abc/.exec(someObject.someSubObject.someValue) : null

@newlukai
Copy link
Author

newlukai commented Mar 5, 2020

I guess you are right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants