From cb3dae2e8fc81cb145a806bad1e546bc17150e7a Mon Sep 17 00:00:00 2001 From: IIITManjeet <96388375+IIITManjeet@users.noreply.github.com> Date: Thu, 27 Oct 2022 13:38:51 +0530 Subject: [PATCH 1/2] Type-Casting example --- Type-Casting/typeCasting.ts | 33 +++++++++++++++++++++++++++++++ package-lock.json | 39 ++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Type-Casting/typeCasting.ts diff --git a/Type-Casting/typeCasting.ts b/Type-Casting/typeCasting.ts new file mode 100644 index 0000000..0acd545 --- /dev/null +++ b/Type-Casting/typeCasting.ts @@ -0,0 +1,33 @@ +/** Steps to run the file +1) First install Typescript: `npm install -g typescript` +2) Move to Type-Casting folder +3) In command prompt run `tsc typeCasting.ts` +4) Run the command `node date.js`*/ + +/*Type Casting can be done using- +1) using 'as' keyword +2) using '<>' operator +3) using force typecasting*/ + +let text : unknown = "Typescript"; +let value = (text as string).length; +console.log(value); + +/*Here earlier if we assign value the text it would surely gonna throw an error, which is an Object 'unknown'. To solve this problem the type casting is used for which we used as keyword. Thus first we cast the variable to a string so that the string function lenght could tell us the length of string text(earlier was of unknown data type)*/ + +let input = document.querySelector('input') as HTMLInputElement; +let inputValue = input.value; + +/*this helps in TypeScript as this throws an error, since Object is possibly 'null'.. TypeScript has a number of predefined types for query selector outputs, but we can't write let input:HTMLInputElement = ... either, since the input is possibly null. As such, we have to cast the input to HTMLInputElement to get the value:*/ + +let para: unknown = "Typescript"; +console.log((para).length); + +/*the same can also be done using <> operator the reasons are same as above NOTE:This type of casting does not work with TSX files*/ + +/*Next way is to force cast the variable into unknown and then to other desired type this is done to avoid type errors as in this example-*/ +let quote = 499; +console.log(((quote as unknown) as string).length); + + + diff --git a/package-lock.json b/package-lock.json index d6160de..1b8e0b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,9 +1,46 @@ { "name": "learn-typescript", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "learn-typescript", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "typescript": "^4.4.3" + }, + "devDependencies": { + "@types/node": "^16.11.3" + } + }, + "node_modules/@types/node": { + "version": "16.18.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.2.tgz", + "integrity": "sha512-KIGQJyya+opDCFvDSZMNNS899ov5jlNdtN7PypgHWeb8e+5vWISdwTRo/ClsNVlmDihzOGqFyNBDamUs7TQQCA==", + "dev": true + }, + "node_modules/typescript": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", + "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + } + }, "dependencies": { + "@types/node": { + "version": "16.18.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.2.tgz", + "integrity": "sha512-KIGQJyya+opDCFvDSZMNNS899ov5jlNdtN7PypgHWeb8e+5vWISdwTRo/ClsNVlmDihzOGqFyNBDamUs7TQQCA==", + "dev": true + }, "typescript": { "version": "4.4.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", From 8508f25341b925f712f9c68d6c61bc22894993e8 Mon Sep 17 00:00:00 2001 From: Manjeet Pathak <96388375+IIITManjeet@users.noreply.github.com> Date: Sat, 29 Oct 2022 19:28:04 +0530 Subject: [PATCH 2/2] Delete package-lock.json --- package-lock.json | 50 ----------------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 1b8e0b3..0000000 --- a/package-lock.json +++ /dev/null @@ -1,50 +0,0 @@ -{ - "name": "learn-typescript", - "version": "1.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "learn-typescript", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "typescript": "^4.4.3" - }, - "devDependencies": { - "@types/node": "^16.11.3" - } - }, - "node_modules/@types/node": { - "version": "16.18.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.2.tgz", - "integrity": "sha512-KIGQJyya+opDCFvDSZMNNS899ov5jlNdtN7PypgHWeb8e+5vWISdwTRo/ClsNVlmDihzOGqFyNBDamUs7TQQCA==", - "dev": true - }, - "node_modules/typescript": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", - "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==", - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=4.2.0" - } - } - }, - "dependencies": { - "@types/node": { - "version": "16.18.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.2.tgz", - "integrity": "sha512-KIGQJyya+opDCFvDSZMNNS899ov5jlNdtN7PypgHWeb8e+5vWISdwTRo/ClsNVlmDihzOGqFyNBDamUs7TQQCA==", - "dev": true - }, - "typescript": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", - "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==" - } - } -}