diff --git a/types/chrome/index.d.ts b/types/chrome/index.d.ts index a59c0a1a979fa1..c25643a039a93e 100644 --- a/types/chrome/index.d.ts +++ b/types/chrome/index.d.ts @@ -2902,20 +2902,59 @@ declare namespace chrome { injectedScript?: string | undefined; } - interface EvaluationExceptionInfo { - /** Set if the error occurred on the DevTools side before the expression is evaluated. */ - isError: boolean; - /** Set if the error occurred on the DevTools side before the expression is evaluated. */ - code: string; - /** Set if the error occurred on the DevTools side before the expression is evaluated. */ - description: string; - /** Set if the error occurred on the DevTools side before the expression is evaluated, contains the array of the values that may be substituted into the description string to provide more information about the cause of the error. */ - details: any[]; - /** Set if the evaluated code produces an unhandled exception. */ - isException: boolean; - /** Set if the evaluated code produces an unhandled exception. */ - value: string; - } + type EvaluationExceptionInfo = + | { + /** + * Set if the error occurred on the DevTools side before the expression is evaluated. + * + * If `isError` is set to true, a DevTools-side error has occurred, and `code` is set to an error code. + * + * See the docs on `isException` for more details on parsing this object. + */ + isError: true; + /** Set if the error occurred on the DevTools side before the expression is evaluated. */ + code: string; + /** Set if the error occurred on the DevTools side before the expression is evaluated. */ + description: string; + /** Set if the error occurred on the DevTools side before the expression is evaluated, contains the array of the values that may be substituted into the description string to provide more information about the cause of the error. */ + details: any[]; + /** + * Set if the evaluated code produces an unhandled exception. + * + * If `isException` is non-null but not true, and `isError` is true, a DevTools-side error has occurred, and `code` is set to an error code. + * + * If `isException` is set to true, a JavaScript error has occurred, and `value` is set to the string value of the thrown object. + */ + isException?: undefined; + /** Set if the evaluated code produces an unhandled exception. */ + value?: undefined; + } + | { + /** + * Set if the error occurred on the DevTools side before the expression is evaluated. + * + * If `isError` is set to true, a DevTools-side error has occurred, and `code` is set to an error code. + * + * See the docs on `isException` for more details on parsing this object. + */ + isError?: undefined; + /** Set if the error occurred on the DevTools side before the expression is evaluated. */ + code?: undefined; + /** Set if the error occurred on the DevTools side before the expression is evaluated. */ + description?: undefined; + /** Set if the error occurred on the DevTools side before the expression is evaluated, contains the array of the values that may be substituted into the description string to provide more information about the cause of the error. */ + details?: undefined; + /** + * Set if the evaluated code produces an unhandled exception. + * + * If `isException` is non-null but not true, and `isError` is true, a DevTools-side error has occurred, and `code` is set to an error code. + * + * If `isException` is set to true, a JavaScript error has occurred, and `value` is set to the string value of the thrown object. + */ + isException: true; + /** Set if the evaluated code produces an unhandled exception. */ + value: string; + }; /** The ID of the tab being inspected. This ID may be used with {@link chrome.tabs} API. */ const tabId: number; @@ -2935,15 +2974,23 @@ declare namespace chrome { function eval( expression: string, options?: EvalOptions, - ): Promise<{ result: T; exceptionInfo: EvaluationExceptionInfo }>; + ): Promise; function eval( expression: string, - callback?: (result: T, exceptionInfo: EvaluationExceptionInfo) => void, + callback: ( + ...args: + | [result: T, exceptionInfo: undefined] + | [result: undefined, exceptionInfo: EvaluationExceptionInfo] + ) => void, ): void; function eval( expression: string, options: EvalOptions | undefined, - callback?: (result: T, exceptionInfo: EvaluationExceptionInfo) => void, + callback: ( + ...args: + | [result: T, exceptionInfo: undefined] + | [result: undefined, exceptionInfo: EvaluationExceptionInfo] + ) => void, ): void; /** diff --git a/types/chrome/package.json b/types/chrome/package.json index 91da517ec54d30..2c05ee5201a263 100644 --- a/types/chrome/package.json +++ b/types/chrome/package.json @@ -15,38 +15,10 @@ "@types/chrome": "workspace:." }, "owners": [ - { - "name": "Matthew Kimber", - "githubUsername": "matthewkimber" - }, - { - "name": "otiai10", - "githubUsername": "otiai10" - }, - { - "name": "sreimer15", - "githubUsername": "sreimer15" - }, { "name": "MatCarlson", "githubUsername": "MatCarlson" }, - { - "name": "ekinsol", - "githubUsername": "ekinsol" - }, - { - "name": "Brian Wilson", - "githubUsername": "echoabstract" - }, - { - "name": "Sebastiaan Pasma", - "githubUsername": "spasma" - }, - { - "name": "bdbai", - "githubUsername": "bdbai" - }, { "name": "Jason Xian", "githubUsername": "JasonXian" @@ -55,10 +27,6 @@ "name": "userTim", "githubUsername": "usertim" }, - { - "name": "Idan Zeierman", - "githubUsername": "idan315" - }, { "name": "Nicolas Rodriguez", "githubUsername": "nicolas377" diff --git a/types/chrome/test/index.ts b/types/chrome/test/index.ts index fd9dbf08e7c0cc..c4110c6f8783b5 100644 --- a/types/chrome/test/index.ts +++ b/types/chrome/test/index.ts @@ -1988,7 +1988,7 @@ function testDevtoolsPanels() { // https://developer.chrome.com/docs/extensions/reference/api/devtools/inspectedWindow function testDevtoolsInspectedWindow() { - const expression = "expression"; + const expression = "typeof jQuery !== 'undefined'"; const evalOptions: chrome.devtools.inspectedWindow.EvalOptions = { frameURL: "https://example.com", @@ -1996,23 +1996,44 @@ function testDevtoolsInspectedWindow() { useContentScriptContext: true, }; - chrome.devtools.inspectedWindow.eval(expression); // $ExpectType Promise<{ result: { [key: string]: unknown; }; exceptionInfo: EvaluationExceptionInfo}> - chrome.devtools.inspectedWindow.eval(expression, evalOptions); // $ExpectType Promise<{ result: { [key: string]: unknown; }; exceptionInfo: EvaluationExceptionInfo}> + chrome.devtools.inspectedWindow.eval(expression); // $ExpectType Promise<{ [key: string]: unknown; }> + chrome.devtools.inspectedWindow.eval(expression, evalOptions); // $ExpectType Promise<{ [key: string]: unknown; }> chrome.devtools.inspectedWindow.eval(expression, evalOptions, (result, exceptionInfo) => { // $ExpectType void - result; // $ExpectType { [key: string]: unknown; } + result; // $ExpectType { [key: string]: unknown; } | undefined + exceptionInfo; // $ExpectType EvaluationExceptionInfo | undefined + + if (result) { + exceptionInfo; // $ExpectType undefined + } - exceptionInfo.code; // $ExpectType string - exceptionInfo.description; // $ExpectType string - exceptionInfo.details; // $ExpectType any[] - exceptionInfo.isError; // $ExpectType boolean - exceptionInfo.isException; // $ExpectType boolean - exceptionInfo.value; // $ExpectType string + if (exceptionInfo) { + result; // $ExpectType undefined + + if (exceptionInfo.isException) { + exceptionInfo.code; // $ExpectType undefined + exceptionInfo.description; // $ExpectType undefined + exceptionInfo.details; // $ExpectType undefined + exceptionInfo.isError; // $ExpectType undefined + exceptionInfo.isException; // $ExpectType true + exceptionInfo.value; // $ExpectType string + } else { + exceptionInfo.code; // $ExpectType string + exceptionInfo.description; // $ExpectType string + exceptionInfo.details; // $ExpectType any[] + exceptionInfo.isError; // $ExpectType true + exceptionInfo.isException; // $ExpectType undefined + exceptionInfo.value; // $ExpectType undefined + } + } }); chrome.devtools.inspectedWindow.eval(expression, (result, exceptionInfo) => { // $ExpectType void - result; // $ExpectType { [key: string]: unknown; } + result; // $ExpectType { [key: string]: unknown; } | undefined + exceptionInfo; // $ExpectType EvaluationExceptionInfo | undefined }); - chrome.devtools.inspectedWindow.eval<{ title: string }>(expression, evalOptions, (result) => { // $ExpectType void - result.title; // $ExpectType string + chrome.devtools.inspectedWindow.eval<{ title: string }>(expression, evalOptions, (result, _) => { // $ExpectType void + if (result) { + result.title; // $ExpectType string + } }); chrome.devtools.inspectedWindow.getResources(); // $ExpectType Promise