Skip to content

ref(type-guards): narrow down the type #4278

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

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/utils/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Primitive } from '@sentry/types';
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isError(wat: any): boolean {
export function isError(wat: unknown): boolean {
switch (Object.prototype.toString.call(wat)) {
case '[object Error]':
return true;
Expand All @@ -29,7 +29,7 @@ export function isError(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isErrorEvent(wat: any): boolean {
export function isErrorEvent(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object ErrorEvent]';
}

Expand All @@ -40,7 +40,7 @@ export function isErrorEvent(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isDOMError(wat: any): boolean {
export function isDOMError(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object DOMError]';
}

Expand All @@ -51,7 +51,7 @@ export function isDOMError(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isDOMException(wat: any): boolean {
export function isDOMException(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object DOMException]';
}

Expand All @@ -62,7 +62,7 @@ export function isDOMException(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isString(wat: any): boolean {
export function isString(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object String]';
}

Expand All @@ -73,7 +73,7 @@ export function isString(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isPrimitive(wat: any): wat is Primitive {
export function isPrimitive(wat: unknown): wat is Primitive {
return wat === null || (typeof wat !== 'object' && typeof wat !== 'function');
}

Expand All @@ -84,7 +84,7 @@ export function isPrimitive(wat: any): wat is Primitive {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isPlainObject(wat: any): boolean {
export function isPlainObject(wat: unknown): wat is Record<string, unknown> {
return Object.prototype.toString.call(wat) === '[object Object]';
}

Expand All @@ -95,7 +95,7 @@ export function isPlainObject(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isEvent(wat: any): boolean {
export function isEvent(wat: unknown): wat is Event {
return typeof Event !== 'undefined' && isInstanceOf(wat, Event);
}

Expand All @@ -106,7 +106,7 @@ export function isEvent(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isElement(wat: any): boolean {
export function isElement(wat: unknown): wat is Element {
return typeof Element !== 'undefined' && isInstanceOf(wat, Element);
}

Expand All @@ -117,15 +117,15 @@ export function isElement(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isRegExp(wat: any): boolean {
export function isRegExp(wat: unknown): wat is RegExp {
return Object.prototype.toString.call(wat) === '[object RegExp]';
}

/**
* Checks whether given value has a then function.
* @param wat A value to be checked.
*/
export function isThenable(wat: any): boolean {
export function isThenable(wat: any): wat is PromiseLike<any> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return Boolean(wat && wat.then && typeof wat.then === 'function');
}
Expand All @@ -137,7 +137,7 @@ export function isThenable(wat: any): boolean {
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isSyntheticEvent(wat: any): boolean {
export function isSyntheticEvent(wat: unknown): boolean {
return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat;
}
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function getWalkSource(
currentTarget?: unknown;
}

const event = value as SimpleEvent;
const event = (value as unknown) as SimpleEvent;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types overlap, TS now properly warns us about it, so we need to cast to unknown first


const source: {
[key: string]: any;
Expand Down