TypeScript
TypeScript Object Oriented Programming
https://github.com/nodejs-projects-kenanhancer/typescript-object-oriented-programming
See the Pen TypeScriptOOPDemo1 by kenanhancer (@kenanhancer) on CodePen.
How to create and run TypeScript project
$ mkdir typescript-demo1
$ npm init -y
$ npm install --save-dev typescript @types/node rimraf
$ npx tsc --init --rootDir src --outDir build \
--esModuleInterop --resolveJsonModule --lib es6 \
--module commonjs --allowJs true --noImplicitAny true
$ mkdir src
$ echo "console.log('Hello world');" > src/index.ts
$ npx tsc
$ node dist/index.js
Continue reading TypeScript Interface Function Types and Indexable Types
Interface as Type
Implementing Interface
Demo1:
See the Pen TypeScriptInterfaceDemo1 by kenanhancer (@kenanhancer) on CodePen.
Continue readingTypeScript Utility Types
You can see examples in the pictures and more demo codes in the rest of post.
Type Inference
ConstructorParameters<T>
Syntax
Examples
InstanceType<T>
Syntax
Examples
ReturnType<T>
Syntax
Examples
Parameters<T>
Syntax
Examples
NonNullable<T>
Syntax
Examples
Extract<T, U>
Syntax
Examples
Exclude<T, U>
Syntax
Examples
Demo 1
Continue readingTypeScript Usage of Namespace, Interface, Class, Abstract Class, Type Alias
Demo 1
Demo 2
TypeScript Enum to Builder Pattern
I just want to create some demo for enum to create a builder . I improved this concept step by step in demos. Notice that all the codes are doing same job but in a different way.
Demo 1
Continue readingTypeScript Syntax
Type Aliases, Union Types, Literal Types, Index Signature
You can find different usages of Type Aliases in the following demo code.
Type Aliases is defined with type
word.
Union Types is defined with the |
character to separate the different possible types.
Literal Types can be string, number or boolean
type SelectedEvent = "Click"; // string literal type
const buttonEvent: SelectedEvent = "Click";
type Color = "Red" | "Green" | "Blue"; // string literal type with union type
const buttonForeColor: Color = "Red";
type num = 1 | 3 | 5 | 7 | 9; // number literal type with union type
type bool = true | false; // boolean literal type with union type
type TRUE = true;
type FALSE = false;
Index Signature is defined as below
type Dictionary = {
[index: string]: any;
};
const person: Dictionary = {
"firstName": "Kenan",
"lastName": "Hancer",
"age": 36
};
interface StringArray {
[index: number]: string;
}
const names: StringArray = ["Bob", "Fred"];
let name1: string = names[0];
Type Aliases can be used with Union Types and Literal Types as below
type obj = {success: true} | {success: false}; // object
type Result<T> = { success: true, value: T } | { success: false, error: string | number }; // object
type PersonCommonFields = { firstName: string, lastName: string };
type Person = PersonCommonFields & { isDeleted: true | false };
type Name = string; // simple type
type NameResolver = () => string; // function
type NameOrResolver = Name | NameResolver;