-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,10 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# OSX | ||
.DS_Store | ||
|
||
|
||
# Project specific | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |