Typescript: what is a type url?

I want to make sure that the type string interface member is an officially valid URL. I can declare the member as a URL, but I cannot assign it a string, which is a valid URL.

interface test {
    myurl: URL;
}

var a : test;
a.myurl = "http://www.google.ch"

When compiling, I get:

The type 'string' is not assigned to the type 'URL'.

Do I need to use decorators for my task ( https://www.typescriptlang.org/docs/handbook/decorators.html )?

What is a URL for?

I am using typescript 1.8.10

+4
source share
1 answer

AFAICT, URL- typescript "" , WhatWG Url. , .

URL-, , . URL-.

Typescript , ( typescript 2.1.5): 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;
}

:

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

, WhatWG Url.

+5

All Articles