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

No type info available except "isEnabled" #158

Closed
foobardevpro opened this issue Oct 12, 2019 · 3 comments
Closed

No type info available except "isEnabled" #158

foobardevpro opened this issue Oct 12, 2019 · 3 comments

Comments

@foobardevpro
Copy link

Any property besides isEnabled gives an error like this (same for request, on, exit, etc):

TS2339: Property 'isFullscreen' does not exist on type 'Screenfull | { isEnabled: false; }'.
  Property 'isFullscreen' does not exist on type '{ isEnabled: false; }'.

Issue is solved if I change:
https://github.com/sindresorhus/screenfull.js/blob/b2a50cc4d1554ef62530a4cae973096a96921c93/src/screenfull.d.ts#L157
to:

declare const screenfull: screenfull.Screenfull;
@sindresorhus
Copy link
Owner

Per readme, you have to guard the usage:

if (screenfull.isEnabled) {
	screenfull.request();
}

@beac0n
Copy link

beac0n commented Feb 23, 2021

Per readme, you have to guard the usage:

if (screenfull.isEnabled) {
	screenfull.request();
}

That is some smart typing. Really neat. I learned something today :-).

I'll explain it to anyone who comes across this issue and scratches their head, like I have:

declare const screenfull: screenfull.Screenfull | {isEnabled: false};

This type definition prevents users from using screenfull.isFullscreen or any other property of screenfull.Screenfull, if screnfull.isEnabled isn't checked first.

It works, because if the user uses screenfull.isFullscreen as is, typescript tries to find isFullscreen in the type {isEnabled: false}, which fails, because isFullscreen doesn't exist on {isEnabled: false}.

If the user checks isEnabled for true first, typescript deduces that the type can not be {isEnabled: false} and uses the type screenfull.Screenfull instead.

@loicraux
Copy link

If you don't want to wrap all your screenfull API calls with an if guard statement, you call also use an assertion function (provided that you use TS >= 3.7) :

function assert(condition: boolean, message?: string): asserts condition {
    if (!condition) {
        // eslint-disable-next-line no-console
        console.trace();
        throw new Error(message);
    }
}

// Some button in your DOM to switch to fullscreen :
const switchToFullScreenButton: HTMLButtonElement = .... ;

switchToFullScreenButton.addEventListener('click', handleClickOnFullscreenButton);
switchToFullScreenButton.disabled = !screenfull.isEnabled;

function handleClickOnFullscreenButton(): void {
    // Here you expect screenfull.isEnabled to be true, so you can assert :
    assert(screenfull.isEnabled);

    // In the rest of the code of this handler, you can use the complete screenfull API,
    if (screenfull.isFullscreen) {
        screenfull.exit();
    } else {
        screenfull.request(someElement);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants