Skip to content

Commit

Permalink
add starting examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pero5ar committed Sep 6, 2021
1 parent 424b68d commit e93fc6c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@ dist

# TernJS port file
.tern-port

# OSX
.DS_Store


# Project specific
yarn.lock
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "typescript-examples",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pero5ar/typescript-examples.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/pero5ar/typescript-examples/issues"
},
"homepage": "https://github.com/pero5ar/typescript-examples#readme",
"dependencies": {
"typescript": "^4.4.2"
}
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2020",
"jsx": "preserve",
"strictFunctionTypes": true,
"sourceMap": true
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
53 changes: 53 additions & 0 deletions types101.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// All the basic types one might need

// JS Primitives

let _number: number = 0;
let _string: string = 'str';
let _boolean: boolean = true;
let _symbol: symbol = Symbol();
let _bigint: bigint = BigInt(9007199254740991);
let _undefined: undefined = undefined;
let _null: null = null;

// Objects

let _obj1: object = { a: 'value' };
let _obj2: {} = { a: 'value' };
let _obj3: { [key: string]: any; } = { a: 'value' };
let _obj4: Record<string, any> = { a: 'value' };
let _obj5: { a: string; } = { a: 'value' };

// Functions

let _fun1: Function = (x: number) => x++;
let _fun2: (...args: any[]) => any = (x: number) => x++;
let _fun3: (x: any) => any = (x: number) => x++;
let _fun4: (x: number) => number = (x: number) => x++;

// Any, unknown, never

function getA1(obj: any) {
return obj.a;
}

function getA2(obj: unknown) {
// return obj.a; -> Property 'a' does not exist on type 'unknown'.ts(2339)
if (isObjWithA(obj)) {
obj.a;
}
return undefined;
}

function isObjWithA(obj: unknown): obj is { a: unknown } {
return typeof obj === 'object' && 'a' in obj;
}

function throwError(msg: string): never {
throw new Error(msg);
}

// TS Literals

let _true: true = true;
let _successfulResponse: 200 | 201 | 204 = 200;

0 comments on commit e93fc6c

Please sign in to comment.