Skip to content

Instantly share code, notes, and snippets.

@jacksteamdev
Last active June 3, 2021 18:20
Show Gist options
  • Save jacksteamdev/8bc05b1257b471f2533bb76b4d58f306 to your computer and use it in GitHub Desktop.
Save jacksteamdev/8bc05b1257b471f2533bb76b4d58f306 to your computer and use it in GitHub Desktop.
type WithTabWithId<Type> = Required<
{
[Property in keyof Type]: Type[Property] extends { id?: number }
? Type[Property] & { id: number }
: Type[Property]
}
>
const isWithTabWithId = <T>(args: T): args is WithTabWithId<T> => {
if (!Array.isArray(args)) return false
const tab = args.find(
(x) =>
x !== null &&
typeof x === 'object' &&
'windowId' in x &&
'pinned' in x &&
'active' in x,
) as Tabs.Tab | undefined
if (isUndefined(tab)) {
console.warn('tab is undefined')
return false
} else if (isTabWithId(tab)) {
console.warn('tab.id is undefined')
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment