Transfer to become a reference manual for Typescript programmers (iv)

Source: Internet
Author: User

Generic type

For C # Programmers, Typescript generics are familiar, basically consistent design.

Type constraints

C # uses the WHERE keyword to mark type constraints, and typescript uses the extends keyword within angle brackets for the same effect.

In the following example, Iexample constrains the generics to be imyinterface and his derived class.

If used like this, it can be constrained to inherit both Classone and Classtwo types. very confusing, please pay special attention to it .

This is because the typescript type system is inherently less stringent, and the following chapters explain the Typescript type system in detail

You can also use generics as a type constraint for generics, as follows

Structure type

C # type system is mandatory, the type of the object must display a declaration. Even though two types have exactly the same structure, they are not the same type.

TypeScript the type system is structural, architectural, and hierarchical. Structures of the same type can be considered the same type.

Below is C # An example of

ClassA and the ClassB are completely different types, they are different and must show an inherited interface in order for them to be compatible.

and in TypeScript not in this way, we use the following example to prove it. ClassA ClassB examplec have signature-consistent functions, so they are compatible.

typescript means that you c#

this thing will make the code ever more changeable, if you're familiar with C # or JAVA , which may confuse you.

?

The following example does not require the class keyword and will actually produce the type.

In this example, an anonymous type is generated.

This anonymous type allows the development tool to provide auto-completion and the compiler will check. If you try to assign a value to the name of Obja, the compiler will check to tell you the error. The compiler also infers the type for the array.

Access Decorators

Typescript's access modifiers may give you a feeling of weakness, indeed. He is just a compile-time feature.

Everything in the module is private unless you add the Export keyword. Types that do not have an export keyword can only be applied within a module.

Inside the class, everything is public unless the Private keyword is added. Public is only intended to look explicit.

Typescript's access decorator is just like this, without C # internal and protected this decorator, want C # similar features to give up.

Transfer to become a reference manual for Typescript programmers (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.