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

undefined URL constructor base argument for Safari #34725

Closed
BarryThePenguin opened this issue Oct 24, 2019 · 3 comments
Closed

undefined URL constructor base argument for Safari #34725

BarryThePenguin opened this issue Oct 24, 2019 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@BarryThePenguin
Copy link

BarryThePenguin commented Oct 24, 2019

TypeScript Version: 3.6.4

Though the types are still the same

TypeScript/lib/lib.dom.d.ts

Lines 16068 to 16073 in a03227d

declare var URL: {
prototype: URL;
new(url: string, base?: string | URL): URL;
createObjectURL(object: any): string;
revokeObjectURL(url: string): void;
};

Search Terms:

undefined URL constructor base argument Safari

Code

class FooURL {
  private _url: URL
  
  constructor(url: string, base?: string | URL) {
    this._url = new URL(url, base)
  }
}

// Throws `TypeError: Type error` in Safari
new FooURL('https://example.com')

Expected behavior:

The constructor for URL should not allow undefined constructor argument for base

Something more appropriate could look like

new(url: string): URL; 
new(url: string, base: string | URL): URL; 

Actual behavior:

Both Safari Version 13.0.2 (14608.2.40.1.3), and Safari Technology Preview Release 94 (Safari 13.1, WebKit 14609.1.6.1) throw an error when the base constructor argument is undefined.

Essentially typescript treats new URL('example.com', undefined) as valid

Other browsers are able to handle this case just fine.

I did look at the whatwg spec

Maybe it's a problem with how TSJS-lib-generator generates optional arguments?

I'm not sure if this is something that should be handled by TypeScript. Interested to hear what others think!

Playground Link

Related Issues:

#15246

@trotyl
Copy link

trotyl commented Oct 25, 2019

I have report this to core-js that Safari doesn't have valid URL implementation: zloirock/core-js#656, the types are indeed correct.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Oct 30, 2019
@RyanCavanaugh
Copy link
Member

TS doesn't really distinguish between passing undefined to a function vs skipping the argument entirely. This is convenient 95% of the time and the remaining 5% breaks libraries that are written in an indirection-hostile way. Anyway we're not here to encode Safari's spec compliance bugs into our lib file.

@BarryThePenguin
Copy link
Author

Ahk, I was unsure if that was the case. That clarifies things for me. Thanks for the response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants