native Android, hybrid iOS. will change to native iOS as soon as I get my hands on macOS. I write codes literally the entire day, if not, I'm looking at codes.
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"?
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).
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.
native Android, hybrid iOS. will change to native iOS as soon as I get my hands on macOS. I write codes literally the entire day, if not, I'm looking at codes.
native Android, hybrid iOS. will change to native iOS as soon as I get my hands on macOS. I write codes literally the entire day, if not, I'm looking at codes.
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:
letx=5;
Javascript figures out it is a number. However, if we wrote:
letx="5"
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:
letx:number=5;
Now if "x" is not a number, we'll get an error. That way we can avoid bugs in our code.
native Android, hybrid iOS. will change to native iOS as soon as I get my hands on macOS. I write codes literally the entire day, if not, I'm looking at codes.
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"?
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 theinterface
.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).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 theninterface
can make sense.While classes are types not all types are classes.
Thank you for the detailed explanation, appreciated so much
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.
thank you for the tip
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:
Javascript figures out it is a number. However, if we wrote:
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:
Now if "x" is not a number, we'll get an error. That way we can avoid bugs in our code.
thank you so much