-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.TypeScriptNode.ts
34 lines (27 loc) · 1.27 KB
/
22.TypeScriptNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// ts-node
// Declare a variable named 'greeting' with a type annotation of 'string'.
// Type annotations in TypeScript help ensure that variables hold specific types of values.
let greeting: string = "Hi, TypeScript learners!";
// Use the 'displayMessage' function to log the greeting to the console.
displayMessage(greeting);
/**
* A simple function to display a message on the console.
*
* @param text - The message to be displayed.
*/
function displayMessage(text: string): void {
console.log(text);
}
// Running this TypeScript file directly using Node.js will result in an error.
// node src/learningTypeScript.ts --> Gives error
// To execute this file using Node.js, you first need to compile it to JavaScript.
// npx tsc src/learningTypeScript.ts
// node src/learningTypeScript.js
// Alternatively, you can use 'ts-node' to directly run TypeScript files without compiling.
// npx ts-node src/learningTypeScript.ts --> Runs if you have the ts-node package installed.
// npm install -g ts-node --> Install ts-node globally to use it from anywhere.
// For convenience, you can modify your 'package.json' to include a script to run this file using ts-node.
// "scripts": {
// "begin": "ts-node src/learningTypeScript.ts"
// },
// Use 'npm run begin' to execute the script.