Re-declarations and Shadowing

Re-declarations and Shadowing With var declarations, we mentioned that it didn’t matter how many times you declared your variables; you just got one. function f(x) { var x; var x; if (true) { var x; } } In the above example, all declarations of x actually refer to the same x, and this is perfectly valid. This often ends up being a source of bugs. Thankfully, let declarations are not as forgiving. let x = 10; let x = 20; // error: can't re-declare 'x' in the same scope The variabl

Public by default

Public by default In our examples, we’ve been able to freely access the members that we declared throughout our programs. If you’re familiar with classes in other languages, you may have noticed in the above examples we haven’t had to use the word public to accomplish this; for instance, C# requires that each member be explicitly labeled public to be visible. In TypeScript, each member is public by default. You may still mark a member public explicitly. We could have written the Animal class fr

Publish to @types

Publish to @types Packages on under the @types organization are published automatically from DefinitelyTyped using the types-publisher tool. To get your declarations published as an @types package, please submit a pull request to https://github.com/DefinitelyTyped/DefinitelyTyped. You can find more details in the contribution guidelines page.

Preventing Name Conflicts

Preventing Name Conflicts Note that it’s possible to define many types in the global scope when writing a global declaration file. We strongly discourage this as it leads to possible unresolvable name conflicts when many declaration files are in a project. A simple rule to follow is to only declare types namespaced by whatever global variable the library defines. For example, if the library defines the global value ‘cats’, you should write declare namespace cats { interface KittySettings { }

Polymorphic this types

Polymorphic this types A polymorphic this type represents a type that is the subtype of the containing class or interface. This is called F-bounded polymorphism. This makes hierarchical fluent interfaces much easier to express, for example. Take a simple calculator that returns this after each operation: class BasicCalculator { public constructor(protected value: number = 0) { } public currentValue(): number { return this.value; } public add(operand: number): this { this.value +

Publicize your declaration file

Publicize your declaration file After publishing your declaration file with your package, make sure to add a reference to it in the DefinitelyTyped repo external package list. Adding this will allow search tools to know that your package provides its own declarations.

Property Decorators

Property Decorators A Property Decorator is declared just before a property declaration. A property decorator cannot be used in a declaration file, or in any other ambient context (such as in a declare class). The expression for the property decorator will be called as a function at runtime, with the following two arguments: Either the constructor function of the class for a static member, or the prototype of the class for an instance member. The name of the member. NOTE A Property Descripto

Private and protected members in classes

Private and protected members in classes Private and protected members in a class affect their compatibility. When an instance of a class is checked for compatibility, if the instance contains a private member, then the target type must also contain a private member that originated from the same class. Likewise, the same applies for an instance with a protected member. This allows a class to be assignment compatible with its super class, but not with classes from a different inheritance hierarc

Parameter properties

Parameter properties In our last example, we had to declare a readonly member name and a constructor parameter theName in the Octopus class, and we then immediately set name to theName. This turns out to be a very common practice. Parameter properties let you create and initialize a member in one place. Here’s a further revision of the previous Octopus class using a parameter property: class Octopus { readonly numberOfLegs: number = 8; constructor(readonly name: string) { } } Notice how

Parameter Decorators

Parameter Decorators A Parameter Decorator is declared just before a parameter declaration. The parameter decorator is applied to the function for a class constructor or method declaration. A parameter decorator cannot be used in a declaration file, an overload, or in any other ambient context (such as in a declare class). The expression for the parameter decorator will be called as a function at runtime, with the following three arguments: Either the constructor function of the class for a sta