diff --git a/src/roadmaps/typescript/content/101-typescript-types/100-boolean.md b/src/roadmaps/typescript/content/101-typescript-types/100-boolean.md index 522544a96..b156b03e7 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/100-boolean.md +++ b/src/roadmaps/typescript/content/101-typescript-types/100-boolean.md @@ -1 +1,14 @@ -# Boolean \ No newline at end of file +# Boolean + +`boolean` is a primitive data type in TypeScript that represents a truth value, either true or false. + +For example: + + ``` + let isTrue: boolean = true; + let isFalse: boolean = false; + ``` + +Learn more from the following links: + +- [Number, String, Boolean, Symbol and Object](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#number-string-boolean-symbol-and-object) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/101-number.md b/src/roadmaps/typescript/content/101-typescript-types/101-number.md index 149fcc78c..529a724ba 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/101-number.md +++ b/src/roadmaps/typescript/content/101-typescript-types/101-number.md @@ -1 +1,16 @@ -# Number \ No newline at end of file +# Number + +It is a primitive data type in TypeScript that represents numeric values. It includes both integer and floating-point values. + +For example: + + ``` + let intValue: number = 42; + let floatValue: number = 3.14; + ``` + +In TypeScript, numbers can be assigned to variables, passed as arguments to functions, and returned from functions. They are also compatible with arithmetic operators such as +, -, *, /, and % (modulus). + +Learn more from the following links: + +- [Number, String, Boolean, Symbol and Object](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#number-string-boolean-symbol-and-object) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/102-string.md b/src/roadmaps/typescript/content/101-typescript-types/102-string.md index f3ad14fe7..499ebf7e6 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/102-string.md +++ b/src/roadmaps/typescript/content/101-typescript-types/102-string.md @@ -1 +1,13 @@ -# String \ No newline at end of file +# String + +In TypeScript, the string is sequence of char values and also considered as an object. + +Syntax: + +``` +var var_name = new String(string); +``` + +Learn more from the following links: + +- [Number, String, Boolean, Symbol and Object](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#number-string-boolean-symbol-and-object) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/103-void.md b/src/roadmaps/typescript/content/101-typescript-types/103-void.md index 436812629..62d700842 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/103-void.md +++ b/src/roadmaps/typescript/content/101-typescript-types/103-void.md @@ -1 +1,18 @@ -# Void \ No newline at end of file +# Void + +Void is used where there is no data. For example, if a function does not return any value then you can specify void as return type. + +Example: + + ``` + function sayHi(): void { + console.log('Hi!') + } + + let speech: void = sayHi(); + console.log(speech); //Output: undefined + ``` + +Learn more from the following links: + +- [void](https://www.typescriptlang.org/docs/handbook/2/functions.html#void) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/104-undefined.md b/src/roadmaps/typescript/content/101-typescript-types/104-undefined.md index e0e40d570..0a1e3bbcd 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/104-undefined.md +++ b/src/roadmaps/typescript/content/101-typescript-types/104-undefined.md @@ -1 +1,20 @@ -# Undefined \ No newline at end of file +# Undefined + +In TypeScript, undefined is a built-in type that represents the absence of a value. It can be assigned to variables, properties, or function return values when there is no meaningful value to return. + +For example: + + ``` + let x: undefined; + x = undefined; // valid + x = null; // not valid + + function doSomething(): undefined { + // ... + return undefined; + } + ``` + +Learn more from the following links: + +- [null and undefined](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#null-and-undefined) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/105-null.md b/src/roadmaps/typescript/content/101-typescript-types/105-null.md index 30e17bd1a..171dc28b3 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/105-null.md +++ b/src/roadmaps/typescript/content/101-typescript-types/105-null.md @@ -1 +1,13 @@ -# Null \ No newline at end of file +# Null + +In TypeScript, both undefined and null actually have their types named undefined and null respectively. Much like void, they’re not extremely useful on their own: + +``` +// Not much else we can assign to these variables! +let u: undefined = undefined; +let n: null = null; +``` + +Learn more from the following links: + +- [Null and Undefined](https://www.typescriptlang.org/docs/handbook/basic-types.html#null-and-undefined) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/106-interface.md b/src/roadmaps/typescript/content/101-typescript-types/106-interface.md index f43fa9e2b..19a890d29 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/106-interface.md +++ b/src/roadmaps/typescript/content/101-typescript-types/106-interface.md @@ -1 +1,17 @@ -# Interface \ No newline at end of file +# Interface + +TypeScript allows you to specifically type an object using an interface that can be reused by multiple objects. + +Example: + + ``` + interface Publication { + isbn: string; + author: string; + publisher: string; + } + ``` + +Learn more from the following links: + +- [Reusable Types (Interfaces)](https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html#reusable-types-interfaces) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/107-class.md b/src/roadmaps/typescript/content/101-typescript-types/107-class.md index 5bd198402..72de5620e 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/107-class.md +++ b/src/roadmaps/typescript/content/101-typescript-types/107-class.md @@ -1 +1,25 @@ -# Class \ No newline at end of file +# Class + +In TypeScript, a class is a blueprint for creating objects with specific properties and methods. Classes are a fundamental concept in object-oriented programming. Here is an example of a simple class in TypeScript: + + ``` + class Car { + make: string; + model: string; + year: number; + + constructor(make: string, model: string, year: number) { + this.make = make; + this.model = model; + this.year = year; + } + + drive() { + console.log(`Driving my ${this.year} ${this.make} ${this.model}`); + } + } + ``` + +Learn more from the following links: + +- [Classes](https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html#classes) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/108-enum.md b/src/roadmaps/typescript/content/101-typescript-types/108-enum.md index ab5f9de0e..0cf02a7cc 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/108-enum.md +++ b/src/roadmaps/typescript/content/101-typescript-types/108-enum.md @@ -1 +1,18 @@ -# Enum \ No newline at end of file +# Enum + +Enums is not a type-level extension of JavaScript. It allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums. + +Example: + + ``` + enum Direction { + Up = 1, + Down, + Left, + Right, + } + ``` + +Learn more from the following links: + +- [TypeScript - Enums](https://www.typescriptlang.org/docs/handbook/enums.html) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/109-array.md b/src/roadmaps/typescript/content/101-typescript-types/109-array.md index 0a0e30ae6..beeab72fd 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/109-array.md +++ b/src/roadmaps/typescript/content/101-typescript-types/109-array.md @@ -1 +1,7 @@ -# Array \ No newline at end of file +# Array + +To specify the type of an array like [1, 2, 3], you can use the syntax number[]; this syntax works for any type (e.g. string[] is an array of strings, and so on). You may also see this written as `Array`, which means the same thing. We’ll learn more about the syntax T when we cover generics. + +Learn more from the following links: + +- [Arrays](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#arrays) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/110-tuple.md b/src/roadmaps/typescript/content/101-typescript-types/110-tuple.md index f37bec8cb..ee414a5b9 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/110-tuple.md +++ b/src/roadmaps/typescript/content/101-typescript-types/110-tuple.md @@ -1 +1,11 @@ -# Tuple \ No newline at end of file +# Tuple + +A tuple type is another sort of Array type that knows exactly how many elements it contains, and exactly which types it contains at specific positions. + + ``` + type StringNumberPair = [string, number]; + ``` + +Learn more from the following links: + +- [Tuple Types](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/111-any.md b/src/roadmaps/typescript/content/101-typescript-types/111-any.md index eb1e533b8..a258415e1 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/111-any.md +++ b/src/roadmaps/typescript/content/101-typescript-types/111-any.md @@ -1 +1,19 @@ -# Any \ No newline at end of file +# Any + +With `any` you can access any properties of it (which will in turn be of type any), call it like a function, assign it to (or from) a value of any type, or pretty much anything else that’s syntactically legal: + + ``` + let obj: any = { x: 0 }; + // None of the following lines of code will throw compiler errors. + // Using `any` disables all further type checking, and it is assumed + // you know the environment better than TypeScript. + obj.foo(); + obj(); + obj.bar = 100; + obj = "hello"; + const n: number = obj; + ``` + +Learn more from the following links: + +- [any](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/112-object.md b/src/roadmaps/typescript/content/101-typescript-types/112-object.md index 91beadf79..7bc788056 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/112-object.md +++ b/src/roadmaps/typescript/content/101-typescript-types/112-object.md @@ -1 +1,19 @@ -# Object \ No newline at end of file +# Object + +To define an object type, we simply list its properties and their types. + +For example, here’s a function that takes a point-like object: + + ``` + // The parameter's type annotation is an object type + function printCoord(pt: { x: number; y: number }) { + console.log("The coordinate's x value is " + pt.x); + console.log("The coordinate's y value is " + pt.y); + } + printCoord({ x: 3, y: 7 }); + ``` + +Learn more from the following links: + +- [Number, String, Boolean, Symbol and Object](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#number-string-boolean-symbol-and-object) +- [Object Types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#object-types) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/113-unknown.md b/src/roadmaps/typescript/content/101-typescript-types/113-unknown.md index 0302b941b..931aa63af 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/113-unknown.md +++ b/src/roadmaps/typescript/content/101-typescript-types/113-unknown.md @@ -1 +1,19 @@ -# Unknown \ No newline at end of file +# Unknown + +The unknown type represents any value. This is similar to the any type, but is safer because it’s not legal to do anything with an unknown value: + + ``` + function f1(a: any) { + a.b(); // OK + } + function f2(a: unknown) { + a.b(); + Object is of type 'unknown'. + } + ``` + +This is useful when describing function types because you can describe functions that accept any value without having any values in your function body. + +Learn more from the following links: + +- [Unknown](https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/114-never.md b/src/roadmaps/typescript/content/101-typescript-types/114-never.md index 870169df3..d4f8d2946 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/114-never.md +++ b/src/roadmaps/typescript/content/101-typescript-types/114-never.md @@ -1 +1,28 @@ -# Never \ No newline at end of file +# Never + +The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns. Variables also acquire the type never when narrowed by any type guards that can never be true. + +The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). Even any isn’t assignable to never. + +Examples of functions returning never: + + ``` + // Function returning never must not have a reachable end point + function error(message: string): never { + throw new Error(message); + } + + // Inferred return type is never + function fail() { + return error("Something failed"); + } + + // Function returning never must not have a reachable end point + function infiniteLoop(): never { + while (true) {} + } + ``` + +Learn more from the following links: + +- [Never](https://www.typescriptlang.org/docs/handbook/basic-types.html#never) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md index d3953193d..117327c2a 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md +++ b/src/roadmaps/typescript/content/101-typescript-types/115-type-assertions/index.md @@ -4,8 +4,8 @@ Type assertions in TypeScript are a way to tell the compiler to treat a value as There are two syntaxes for type assertions in TypeScript: -1. The "angle-bracket" syntax: value -2. The "as" syntax: value as T +1. The "angle-bracket" syntax: `value` +2. The "as" syntax: value as `T` For example: diff --git a/src/roadmaps/typescript/content/101-typescript-types/116-satisfies-keyword.md b/src/roadmaps/typescript/content/101-typescript-types/116-satisfies-keyword.md index 06eeb4a6b..0dc9a5a45 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/116-satisfies-keyword.md +++ b/src/roadmaps/typescript/content/101-typescript-types/116-satisfies-keyword.md @@ -1 +1,23 @@ -# Satisfies keyword \ No newline at end of file +# Satisfies Keyword + +TypeScript developers are often faced with a dilemma: we want to ensure that some expression matches some type, but also want to keep the most specific type of that expression for inference purposes. + +For example: + + ``` + // Each property can be a string or an RGB tuple. + const palette = { + red: [255, 0, 0], + green: "#00ff00", + bleu: [0, 0, 255] + // ^^^^ sacrebleu - we've made a typo! + }; + // We want to be able to use array methods on 'red'... + const redComponent = palette.red.at(0); + // or string methods on 'green'... + const greenNormalized = palette.green.toUpperCase(); + ``` + +Learn more from the following links: + +- [Satisfies Keyword](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html#the-satisfies-operator) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/101-typescript-types/index.md b/src/roadmaps/typescript/content/101-typescript-types/index.md index 19e606eab..90c24f608 100644 --- a/src/roadmaps/typescript/content/101-typescript-types/index.md +++ b/src/roadmaps/typescript/content/101-typescript-types/index.md @@ -1 +1,26 @@ -# Typescript types \ No newline at end of file +# Typescript Types + +TypeScript has several built-in types, including: + +1. number +2. string +3. boolean +4. any +5. void +6. null and undefined +7. never +8. object +9. symbol +10. Enumerated types (enum) +11. Tuple types +12. Array types +13. Union types +14. Intersection types +15. Type aliases +16. Type assertions + +Additionally, you can also create custom types in TypeScript using interfaces, classes, and type aliases. + +Learn more from the following links: + +- [Transcript - Everyday Types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html) \ No newline at end of file