Skip to content

Commit fe64464

Browse files
AI-DEV-BOTisaacs
andauthored
refactor(core): replace public any types with unknown (#21249)
- Replace the remaining `TODO: fix in v11` public `any` types in core type definitions with `unknown` or `Record<string, unknown>`. - Type `isInstanceOf` around `unknown` inputs while keeping the existing invalid-constructor guard behavior. - Remove now-unnecessary `Error` assertions in aggregate error handling after the stricter type guard narrowing. Fixes #18570 Co-authored-by: isaacs <i@izs.me>
1 parent 4a0d519 commit fe64464

7 files changed

Lines changed: 14 additions & 24 deletions

File tree

packages/core/src/types/error.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22
* Just an Error object with arbitrary attributes attached to it.
33
*/
44
export interface ExtendedError extends Error {
5-
// TODO: fix in v11, convert any to unknown
6-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7-
[key: string]: any;
5+
[key: string]: unknown;
86
}

packages/core/src/types/misc.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import type { QueryParams } from './request';
44
* Data extracted from an incoming request to a node server
55
*/
66
export interface ExtractedNodeRequestData {
7-
// TODO: fix in v11, convert any to unknown
8-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9-
[key: string]: any;
7+
[key: string]: unknown;
108

119
/** Specific headers from the request */
1210
headers?: { [key: string]: string };

packages/core/src/types/samplingcontext.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import type { SpanAttributes } from './span';
66
* Context data passed by the user when starting a transaction, to be used by the tracesSampler method.
77
*/
88
export interface CustomSamplingContext {
9-
// TODO: fix in v11, convert any to unknown
10-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11-
[key: string]: any;
9+
[key: string]: unknown;
1210
}
1311

1412
/**

packages/core/src/types/stackframe.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ export interface StackFrame {
1313
in_app?: boolean;
1414
instruction_addr?: string;
1515
addr_mode?: string;
16-
// TODO: fix in v11, convert any to unknown
17-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18-
vars?: { [key: string]: any };
16+
vars?: { [key: string]: unknown };
1917
debug_id?: string;
20-
// TODO: fix in v11, convert any to unknown
21-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22-
module_metadata?: any;
18+
module_metadata?: Record<string, unknown>;
2319
}

packages/core/src/types/user.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
* An interface describing a user of an application or a handled request.
33
*/
44
export interface User {
5-
// TODO: fix in v11, convert any to unknown
6-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7-
[key: string]: any;
5+
[key: string]: unknown;
86
id?: string | number;
97
ip_address?: string | null;
108
email?: string;

packages/core/src/utils/aggregate-errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function aggregateExceptionsFromError(
5757
// Recursively call this function in order to walk down a chain of errors
5858
if (isInstanceOf(error[key], Error)) {
5959
applyExceptionGroupFieldsForParentException(exception, exceptionId, error);
60-
const newException = exceptionFromErrorImplementation(parser, error[key] as Error);
60+
const newException = exceptionFromErrorImplementation(parser, error[key]);
6161
const newExceptionId = newExceptions.length;
6262
applyExceptionGroupFieldsForChildException(newException, key, newExceptionId, exceptionId);
6363
newExceptions = aggregateExceptionsFromError(
@@ -78,7 +78,7 @@ function aggregateExceptionsFromError(
7878
error.errors.forEach((childError, i) => {
7979
if (isInstanceOf(childError, Error)) {
8080
applyExceptionGroupFieldsForParentException(exception, exceptionId, error);
81-
const newException = exceptionFromErrorImplementation(parser, childError as Error);
81+
const newException = exceptionFromErrorImplementation(parser, childError);
8282
const newExceptionId = newExceptions.length;
8383
applyExceptionGroupFieldsForChildException(newException, `errors[${i}]`, newExceptionId, exceptionId);
8484
newExceptions = aggregateExceptionsFromError(

packages/core/src/utils/is.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,13 @@ export function isSyntheticEvent(wat: unknown): boolean {
197197
* @param base A constructor to be used in a check.
198198
* @returns A boolean representing the result.
199199
*/
200-
// TODO: fix in v11, convert any to unknown
201-
// export function isInstanceOf<T>(wat: unknown, base: { new (...args: any[]): T }): wat is T {
202-
export function isInstanceOf<T>(wat: any, base: any): wat is T {
200+
type Constructor<T> = { new (...args: never[]): T };
201+
202+
export function isInstanceOf<T>(wat: unknown, base: Constructor<T>): wat is T;
203+
export function isInstanceOf(wat: unknown, base: unknown): boolean;
204+
export function isInstanceOf<T>(wat: unknown, base: unknown): wat is T {
203205
try {
204-
return wat instanceof base;
206+
return wat instanceof (base as Constructor<T>);
205207
} catch {
206208
return false;
207209
}

0 commit comments

Comments
 (0)