Skip to content

Conversation

@t-winter
Copy link
Contributor

We needed support for Dates in interfaces so I've added it. This only targets the global Date class and does not add any support for any other classes.
I've made it so that the check still happens even if the "ignoreClasses" is set.
The relevant unit tests have been added/changed.

ts.createIdentifier('object'),
ts.createToken(ts.SyntaxKind.InstanceOfKeyword),
ts.createPropertyAccess(
ts.createIdentifier('global'),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work for NodeJS apps, but in the browser global should be window.
If you leave out global/window it'd work on both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't leave it out (to avoid conflicts with other classes of the same name) but I could add a check if global is available and test against window if it ins't. How about that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose my proposal won't work because previously defined custom class Date will trigger a false positive.
I guess a polyfill will need to be added for globalThis which can then be used.
https://blog.logrocket.com/what-is-globalthis-why-use-it/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a check sounds good. How will you achieve it? If I try to reference global in the browser I get a ReferenceError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not yet tried i but my best guess at the moment would be: try to reference it, use it if it works or use window it that get's us the ReferenceError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found nicer a better way that doesn't require to trigger an error:

if (typeof global === 'undefined')
    // use window
else
    // use global

@t-winter
Copy link
Contributor Author

I've changed the implementation of the check as mentioned above.
I also have added the missing "Date" reason in the interface.

@woutervh-
Copy link
Owner

Excellent! I will merge and release this.

@woutervh- woutervh- merged commit 9f6058e into woutervh-:master May 11, 2020
@woutervh-
Copy link
Owner

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

Successfully merging this pull request may close these issues.

2 participants