From 3a91c882dacffa0385ac26d2873c79429f5520ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20H=C3=BCbelbauer?= Date: Sun, 7 Aug 2022 10:37:24 +0200 Subject: [PATCH] Make RegExpMatchArray index and input field non-optional On MDN, I don't see documentation for any case where either of these could be `undefined`. This change dates all the way back to 2017 when @jedmao asked @DanielRosenwasser about it, but there was no response: https://github.com/microsoft/TypeScript/commit/7bf846ab3dd620e182da2bb4dc6f85b23016aecf#diff-88c6092c5611bde0c0df8f538cb4334bcdc2860d73616190f1c3d3095ceda301R794 Either this is a mistake or there indeed is a case where either of these can be `undefined` but if that's the case, can anyone please educate me on that? I will update the MDN page to include information about this as well as add JSDoc description to the fields so that this information appears on hover, too. The fields being optional is problematic because either one has to add a condition that will never be useful for anything or use the `!` operator. The problem with the latter is that it is not obvious why it needs to be there (circling back to the root of this issue) and that TypeScript is also used for type checking JavaScript with JSDoc and JavaScript has no `!`, so code like this won't work in JavaScript checked with TypeScript: ```javascript for (const match of ''.matchAll(/something/g)) { results[match.index] = match[0]; } ``` In a case like this, the only solution is to use `@ts-ignore` and risk missing other erros on that line or to add the presumable useless condition for checking `index` is not `undefined`. --- lib/lib.es5.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 2a70d1d889bd9..39c331b2618c8 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -907,8 +907,8 @@ interface DateConstructor { declare var Date: DateConstructor; interface RegExpMatchArray extends Array { - index?: number; - input?: string; + index: number; + input: string; } interface RegExpExecArray extends Array {