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

core: refine typing of isObject<T> #12259

Merged
merged 5 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/core/src/browser/shell/shell-layout-restorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ export class ShellLayoutRestorer implements CommandContribution {
});
}
return widgets;
} else if (isObject<Record<string, WidgetDescription>>(value) && !Array.isArray(value)) {
} else if (isObject(value) && !Array.isArray(value)) {
const copy: Record<string, unknown> = {};
for (const p in value) {
if (this.isWidgetProperty(p)) {
parseContext.push(async context => {
copy[p] = await this.convertToWidget(value[p], context);
copy[p] = await this.convertToWidget(value[p] as WidgetDescription, context);
});
} else {
copy[p] = value[p];
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
export { ArrayUtils } from './array-utils';
export { Prioritizeable } from './prioritizeable';

type UnknownObject = Record<string | number | symbol, unknown>;
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved

export type Deferred<T> = { [P in keyof T]: Promise<T[P]> };
export type MaybeArray<T> = T | T[];
export type MaybeNull<T> = { [P in keyof T]: T[P] | null };
Expand Down Expand Up @@ -54,7 +56,7 @@ export function isFunction<T extends (...args: unknown[]) => unknown>(value: unk
return typeof value === 'function';
}

export function isObject<T = Record<string | number | symbol, unknown>>(value: unknown): value is T {
export function isObject<T extends object = UnknownObject>(value: unknown): value is UnknownObject & { [K in keyof T]: unknown } {
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-null/no-null
return typeof value === 'object' && value !== null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/markers/src/browser/problem/problem-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ProblemSelection {
}
export namespace ProblemSelection {
export function is(arg: unknown): arg is ProblemSelection {
return isObject<ProblemSelection>(arg) && ProblemMarker.is(arg.marker);
return isObject<ProblemSelection>(arg) && Marker.is(arg.marker) && ProblemMarker.is(arg.marker);
}

export class CommandHandler extends SelectionCommandHandler<ProblemSelection> {
Expand Down
14 changes: 14 additions & 0 deletions packages/markers/src/common/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { isObject, isString } from '@theia/core/lib/common/types';

/*
* A marker represents meta information for a given uri
*/
Expand All @@ -37,3 +39,15 @@ export interface Marker<T> {
*/
data: T;
}
export namespace Marker {
export function is(value: unknown): value is Marker<object>;
export function is<T>(value: unknown, subTypeCheck: (value: unknown) => value is T): value is Marker<T>;
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
export function is(value: unknown, subTypeCheck?: (value: unknown) => boolean): boolean {
subTypeCheck ??= isObject;
return isObject<Marker<object>>(value)
&& !Array.isArray(value)
&& subTypeCheck(value.data)
&& isString(value.uri)
&& isString(value.owner);
}
}
2 changes: 2 additions & 0 deletions packages/plugin-ext/src/plugin/types-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ export class Range {
return true;
}
return isObject<theia.Range>(arg)
&& isObject(arg.start)
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
&& isObject(arg.end)
&& Position.isPosition(arg.start)
&& Position.isPosition(arg.end);
}
Expand Down