Skip to content

undefined URL constructor base argument for Safari #34725

@BarryThePenguin

Description

@BarryThePenguin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions