The isBrowser API in window-ref. Ts encapsulates the Angular standard API isplatformBrowser:

From the @ presents/common:

isBrowser(): boolean { // TODO(#11133): Remove condition when platformId will be always provided return this.platformId ? isPlatformBrowser(this.platformId) : typeof window ! == 'undefined'; }

Here’s the standard API in @angular/common:

https://angular.io/api/common…

After I click in, I can’t see the actual implementation code:

@publicApi

Export Declare Function

Declare vs. var

var creates a new variable. declare is used to tell TypeScript that the variable has been created elsewhere. If you use declare, nothing is added to the JavaScript that is generated – it is simply a hint to the compiler.

For example, if you use an external script that defines var externalModule, you would use declare var externalModule to hint to the TypeScript compiler that externalModule has already been set up

export declare class Action{
...
}

The class’s real implementation is probably in somewhere else – maybe a .js file.

The declare indicates that the actual implementation of the Action must be somewhere else, most likely in some.js file.

The TypeScript help documentation explains the declare keyword:

The TypeScript declare keyword is used to declare variables that may not have originated from a TypeScript file.

The TypeScript declare keyword, which declares a variable whose original definition may not have come from a TypeScript file.

For example, lets imagine that we have a library called myLibrary that doesn’t have a TypeScript declaration file and have a namespace called myLibrary in the global namespace. If you want to use that library in your TypeScript code, you can use the following code:

declare var myLibrary;

The type that The TypeScript runtime will give to myLibrary variable is The any type. The problem here is that you won’t have Intellisense for that variable in design time but you will be able to use the library in your code.

Another option to have the same behavior without using the declare keyword is just using a variable with the any type:

var myLibrary: any;

Both of the code examples will result in the same JavaScript output but the declare example is more readable and expresses an ambient declaration.

Uncaught ReferenceError: MyLibrary is not defined



More of Jerry’s original articles can be found on “Wang Zixi “: