Skip to content

Commit 5c97108

Browse files
committed
Adds is[Any] methods to avoid instanceof checks
1 parent bddcae7 commit 5c97108

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/views/viewBase.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,41 @@ export type View =
7878
| WorkspacesView
7979
| WorktreesView;
8080

81+
// prettier-ignore
82+
type TreeViewByType = {
83+
[T in TreeViewTypes]: T extends 'branches'
84+
? BranchesView
85+
: T extends 'commits'
86+
? CommitsView
87+
: T extends 'contributors'
88+
? ContributorsView
89+
: T extends 'drafts'
90+
? DraftsView
91+
: T extends 'fileHistory'
92+
? FileHistoryView
93+
: T extends 'launchpad'
94+
? LaunchpadView
95+
: T extends 'lineHistory'
96+
? LineHistoryView
97+
: T extends 'pullRequest'
98+
? PullRequestView
99+
: T extends 'remotes'
100+
? RemotesView
101+
: T extends 'repositories'
102+
? RepositoriesView
103+
: T extends 'searchAndCompare'
104+
? SearchAndCompareView
105+
: T extends 'stashes'
106+
? StashesView
107+
: T extends 'tags'
108+
? TagsView
109+
: T extends 'workspaces'
110+
? WorkspacesView
111+
: T extends 'worktrees'
112+
? WorktreesView
113+
: View;
114+
};
115+
81116
export type ViewsWithBranches = BranchesView | CommitsView | RemotesView | RepositoriesView | WorkspacesView;
82117
export type ViewsWithBranchesNode = BranchesView | RepositoriesView | WorkspacesView;
83118
export type ViewsWithCommits = Exclude<View, LineHistoryView | StashesView>;
@@ -124,6 +159,14 @@ export abstract class ViewBase<
124159
>
125160
implements TreeDataProvider<ViewNode>, Disposable
126161
{
162+
is<T extends keyof TreeViewByType>(type: T): this is TreeViewByType[T] {
163+
return this.type === (type as unknown as Type);
164+
}
165+
166+
isAny<T extends (keyof TreeViewByType)[]>(...types: T): this is TreeViewByType[T[number]] {
167+
return types.includes(this.type as unknown as T[number]);
168+
}
169+
127170
get id(): TreeViewIds<Type> {
128171
return `gitlens.views.${this.type}`;
129172
}

0 commit comments

Comments
 (0)