-
-
Notifications
You must be signed in to change notification settings - Fork 694
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Bug: typescript declaration #173
Comments
@sindresorhus same issue, any estimation to fix? Thanks! |
First of all, thanks for making this package. I've found it very helpful in the past :) (screenful as Screenful).toggle(...)
...
(screenful as Screenful).isFullscreen It might just be that I have wrong settings in my tsconfig, but this at least forces the compiler to find the right type definition so I can carry forward. I shouldn't have to do this, though :/. |
Less invasive variant that doesn't cause lots of changes once this is fixed:
|
This is unfortunately a limitation of TypeScript. TS has type guards for things like this, but that doesn't work with properties. Ideally, we would have been able to do specify something like this: readonly isEnabled: this is Screenfull; And then Screenfull would be available if you checked the A possible solution: -declare const screenfull: screenfull.Screenfull | {isEnabled: false};
+declare const screenfull: screenfull.Screenfull | (Partial<screenfull.Screenfull> & {isEnabled: false}); (Untested) This way it would stay safe, but if you know you already checked for access, you can simply do |
The solution is definitely not working, when you access screenFull in a callback function, ts would assume the screenfull may change when you invoke the callback after you check Also it's not a typescript problem at all. As long as |
This is a possible workaround: if (screenfull.isEnabled) {
const realScreenFull = screenfull;
realScreenFull.on('change', () => {
console.log('Am I fullscreen?', realScreenFull .isFullscreen ? 'Yes' : 'No');
});
} As @sindresorhus seems to prefer remaining the A good typescript code disencourage you use the non-null assertion declare const canUse = () => ScreenFull | false; import { canUse } from 'screen-full';
const screenFull = canUse();
if (screenFull) {
.... // screenFull is ScreenFull and is unchangable now
} |
The TS definition just reflects the actual JS code.
You should ideally not use it a lot, but it does make sense when you can be sure it's safe. It is especially useful now that we have the |
I'm ok with removing the |
Hi @sindresorhus, maybe I missed something, but was this fix released? 🤔 This declaration's still in the main branch: https://github.com/sindresorhus/screenfull.js/blob/main/src/screenfull.d.ts#L169 |
Hi @sindresorhus. It's perfectly clear at this point that #154 turned out to be a bad idea. It was added as a nice and simple little improvement, but it's causing various problems and a lot of confusion. I, for example, finally found this issue after beating my head against the wall for almost an hour, I thought there was something wrong with my code editor, I even suspected that this was a TypeScript bug, etc. Please revert it back. Thank you. |
Why are you using
ScreenFull | { isEnabled: false}
, it's causing issues.E.g: below is a direct copy from d.tsfile:
and will throw an error:
This means either I checked
screenfull.isEnabled
every time I access itor I cast it every time:
Above error will appear in every method of screenfull.
You may write this to force the users to check
isEnabled
first, but we have to check it again in every method of screenfull. Please consider changing theScreenFull | { isEnabled: false}
, as the type declaration now is really annoying.The text was updated successfully, but these errors were encountered: