From e93fc6c72c99f5a85a841d0c5084d66fa4d5556d Mon Sep 17 00:00:00 2001 From: pero5ar Date: Mon, 6 Sep 2021 10:34:58 +0200 Subject: [PATCH] add starting examples --- .gitignore | 7 ++++++ .vscode/settings.json | 3 +++ package.json | 22 ++++++++++++++++++ tsconfig.json | 13 +++++++++++ types101.ts | 53 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 package.json create mode 100644 tsconfig.json create mode 100644 types101.ts diff --git a/.gitignore b/.gitignore index 6704566..cb6e21a 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,10 @@ dist # TernJS port file .tern-port + +# OSX +.DS_Store + + +# Project specific +yarn.lock \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..55712c1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..a2ca1e4 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..41e32d9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es2020", + "jsx": "preserve", + "strictFunctionTypes": true, + "sourceMap": true + }, + "exclude": [ + "node_modules", + "**/node_modules/*" + ] +} \ No newline at end of file diff --git a/types101.ts b/types101.ts new file mode 100644 index 0000000..a94df0a --- /dev/null +++ b/types101.ts @@ -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 = { 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; \ No newline at end of file