Typescript: What is type URL?

AFAICT, URL is a typescript “built-in” feature, based on the WhatWG Url specifications. The linked to page has both rationale and examples.

In short it offers a structured way of using urls while making sure that they are valid. It will throw errors when attempting to create invalid urls.

Typescript has the according type-definitions set as follows (as of typescript 2.1.5): in node_modules/typescript/lib/lib.es6.d.ts:

interface URL {
    hash: string;
    host: string;
    hostname: string;
    href: string;
    readonly origin: string;
    password: string;
    pathname: string;
    port: string;
    protocol: string;
    search: string;
    username: string;
    toString(): string;
}

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

For your use-case you should be able to use it like this:

a.myurl = new URL("http://www.google.ch");

More constructors, samples and explanations can be found in the WhatWG Url specifications.

Leave a Comment