DEV Community 👩‍💻👨‍💻

Discussion on: How the TypeScript Required Type Works

Collapse
fathidevs profile image
fathidevs

I heard about typescript couple of weeks ago while talking about Javascript (coz I'm learning it). From my basic js I can say it's similar in syntax, but what's the "type" keyword? Is it like "class"?

Collapse
peerreynders profile image
peerreynders • Edited on

but what's the "type" keyword? Is it like "class"?

No. A class is a template for creating objects.

A type alias is simply a name for a type. A type alias can describe an object type, union type, tuple type or array type.

Typescript is structurally typed. Languages like C# and Java are nominally typed which means that if two types have different names they are always treated as separate types. That is not the case with TypeScript.

If you have two types that go by a different names, TypeScript will treat them as equivalent if they have an identical shape. In the case of an object type the "shape" is derived from the key/value type pairs found on the object (regardless of order).

People more comfortable with class-oriented implementations tend to use interface and then extend them when implementing their classes. But plain objects can satisfy an interface, so there is no need to implement a class; a factory function for creating objects or even an object literal is good enough.

The one unique property of interfaces is that they merge so it's possible to declare an interface in bits and pieces and then objects have to implement all the merged requirements in order to satisfy the interface.

With type types can be intersected which basically combines the requirements of the intersected types (with a type union you only satisfy one of the types that comprise the union).

intersection versus union of types
Source

My personal opinion is that type aliases are more versatile as they can represent more than just object types. However when you are implementing classes and/or need declaration merging then interface can make sense.

While classes are types not all types are classes.

Collapse
fathidevs profile image
fathidevs

Thank you for the detailed explanation, appreciated so much

Collapse
curiousdev profile image
CuriousDev

It should be noted, that TypeScript basically is extended JavaScript. If you are still learning, it makes sense to stick a while to JS only.

Collapse
fathidevs profile image
fathidevs

thank you for the tip

Collapse
smpnjn profile image
Johnny Simpson Author

I'll try to explain what TypeScript is briefly. All data has a type when we program something. For example, 5 is a number, "this-is-a-text" is a string, etc.

Javascript figures out the types by itself - so you never mention them in your code. That means if you write:

let x = 5;
Enter fullscreen mode Exit fullscreen mode

Javascript figures out it is a number. However, if we wrote:

let x = "5"
Enter fullscreen mode Exit fullscreen mode

Javascript will think it's a string, since we put 5 in quotation marks. This can mean in complicated applications, bugs can start to arise because we don't "enforce" types. For example "5" + "5" is 55, but 5 + 5 is 10.

Ideally, we would "enforce" "5" to be a number. TypeScript tries to fix this problem by adding types into Javascript, so they are all defined up front. Instead of the above, we write:

let x:number = 5;
Enter fullscreen mode Exit fullscreen mode

Now if "x" is not a number, we'll get an error. That way we can avoid bugs in our code.

Collapse
fathidevs profile image
fathidevs

thank you so much