Typescript
cotrVar
TypeScript Create Variable
let ${1:myVar} = $2;
cotrVarNullable
TypeScript Create Nullable Variable
let ${1:myVar}: ${2:Type} | null = $3;
cotrVarMapClass
TypeScript Create Map Variable (Map Class)
let ${1:myMap} = new Map<string, ${2:type}>([ ['${3:key1}', ${4:'value1'}], ['${5:key2}', ${6:'value2}']]);
cotrPrintMulti
TypeScript Print Multi
console.log(`${1:Line 1}${2:Line 2}${3:Line 3}`);
cotrTernary
TypeScript Ternary Operator
${1:condition} ? ${2:trueValue} : ${3:falseValue}
cotrTypeCheck
TypeScript Type Check
typeof ${1:variable} // Note: This checks the runtime type.
cotrVarStatic
TypeScript Static Variable
static ${3:myStaticVar}: ${2:type} = ${4:value};
// Access the static variable// MyClass.${3:myStaticVar}
cotrWhileLoop
TypeScript While Loop
while (${1:condition}) { ${2:// Your code here}}
cotrTypes
TypeScript Types
$BLOCK_COMMENT_STARTTypeScript is a statically typed language.
Types in TypeScript include:- number: Numeric data type- string: Textual data type- boolean: True or false value- array: Array of values- tuple: Fixed-length array of values- enum: Enumeration of named values- any: Any data type- void: Absence of a value- null: Null value- undefined: Undefined value- never: Represents values that never occur- object: Non-primitive data type$BLOCK_COMMENT_END
cotrConcat
TypeScript Concatenate Strings
'Hello, ' + ${1:name} + '!'
cotrSwitch
TypeScript Switch Statement
switch (${1:variable}) { case ${2:value1}: ${3:// Your code here} break; case ${4:value2}: ${5:// Your code here} break; default: ${6:// Your code here}}
cotrEnum
TypeScript Enum
enum ${1:MyEnum} { ${2:value1}, ${3:value2}, // Add more values here}
cotrOperatorsBool
TypeScript Boolean Operators
// TypeScript Boolean Operators// Logical AND: &&// Logical OR: ||// Logical NOT: !// Equality: ==// Strict Equality: ===// Inequality: !=// Strict Inequality: !==// Greater than: >// Less than: <// Greater than or equal to: >=// Less than or equal to: <=
cotrVarNumber
TypeScript Create Number Variable
let ${1:myNumber}: number = ${2:0};
cotrOperators
TypeScript Mathematical Operators
// TypeScript Mathematical Operators// Addition: +// Subtraction: -// Multiplication: *// Division: /// Modulus (Remainder): %// Exponentiation: **// Increment: Use ++ or +=// Decrement: Use -- or -=// Assignment: =// Addition assignment: +=// Subtraction assignment: -=// Multiplication assignment: *=// Division assignment: /=// Modulus assignment: %=// Exponentiation assignment: **=
cotrTypesBool
TypeScript Boolean Type
boolean
cotrTypesBoolFalse
TypeScript Boolean False
false
cotrForLoop
TypeScript For Loop
for (let ${1:i} = 0; $1 < ${2:10}; $1++) { // Your code here}
cotrTypeCompare
TypeScript Type Comparison
// Check if two variables have the same type:if (typeof ${1:variable1} === typeof ${2:variable2}) { // Your code here}
cotrThrow
TypeScript Throw Exception
throw new Error('Your message here');
cotrTypeConvert
TypeScript Type Conversion
// TypeScript Type Conversion:
// Implicit conversions (TypeScript performs automatically):// - Can be unpredictable, especially with loose equality (==).
// Explicit conversions:// - variable as Type // Type assertion (can throw an error if conversion fails)// - <Type>variable // Type casting (can throw an error if conversion fails)
// Note:// - Be aware of implicit conversions and use explicit conversions when necessary for clarity and control.
cotrVarMap
TypeScript Create Map Variable
let ${1:myMap}: { [key: string]: ${2:type} } = { ${3:'key1'}: ${4:'value1'}, ${5:'key2'}: ${6:'value2'}, // Add more key-value pairs here};
cotrGenMap
TypeScript Generate Object Map
const ${1:myMap} = Object.fromEntries( Array.from({ length: ${2:length} }, (_, index) => [`key${index}`, `value${index}`]));
cotrFuncArgsNamed
TypeScript Function Named Args
function ${2:myFunction}({${3:arg1}, ${4:arg2}}: {${3:arg1}: ${5:type1}, ${4:arg2}: ${6:type2}}): ${1:void} { ${7:// Your code here}}
cotrIf
TypeScript If Statement
if (${1:condition}) { ${2:// Your code here}}
cotrTypesBoolTrue
TypeScript Boolean True
true
cotrNow
TypeScript Date Now
new Date()
cotrVarString
TypeScript Create String Variable
let ${1:myString}: string = ${2:'myValue'};
cotrFuncArrow
TypeScript Arrow Function
const ${2:myFunction} = (${3:parameters}): ${1:void} => { ${4:// Your code here}};
cotrComment
TypeScript Comment
// ${1:Your comment here}
cotrInterpolate
TypeScript Interpolate String
`Hello, ${1:name}!`
cotrVarDate
TypeScript Create Date Variable
let ${1:myDate}: Date = new Date(${2:year}, ${3:month} - 1, ${4:day});
cotrTryCatch
TypeScript Try Catch
try { ${1:// Your code here}} catch (${2:exception}) { ${3:// Your code here}}
cotrStructure
TypeScript Project Structure (High-Level)
// Recommended High-Level TypeScript Project Structure:
// - src/// - Contains the TypeScript source files.// - Organize code into modules or features.
// - dist/// - Contains the compiled JavaScript files.// - This directory is generated after transpilation.
// - tests/// - Contains test files, often mirroring the structure of the src/ directory.
// - node_modules/// - Contains all the npm dependencies.
// - package.json// - Manages project metadata, scripts, and dependencies.
// - tsconfig.json// - Configuration for the TypeScript compiler.
// - .gitignore// - Specifies intentionally untracked files to ignore.
// - README.md// - Project overview, setup instructions, and other essential information.
// Note:// - Adjust the structure as needed based on project size and complexity.// - Consider separate directories for assets, styles, or utilities if necessary.
cotrConst
TypeScript Create Constant
const ${1:myConst} = $2;
cotrFunc
TypeScript Function
function ${2:myFunction}(${3:parameters}): ${1:void} { ${4:// Your code here}}
cotrFuncLambda
TypeScript Lambda
const ${1:myLambda} = (${2:parameters}) => ${3:expression};
cotrTypesString
TypeScript String Type
string
cotrTypesList
TypeScript List Type
Array<${1:Type}>
cotrFuncAnon
TypeScript Anonymous Function
(${1:parameters}): ${2:void} => { ${3:// Your code here}};
cotrInfo
TypeScript Info
Typing: Statically typed (superset of JavaScript)Paradigm: Multi-paradigm: event-driven, functional, imperative, object-orientedCompilation: Transpiled to JavaScriptConcurrency: Inherits JavaScript's event loop model for asynchronous programming
cotrAny
TypeScript Dynamic Type
any
cotrVarSyntax
Variable Declaration Syntax
// TypeScript Variable Declaration Syntax:
// - var: (Scope: Function or Global)// - Can be reassigned and redeclared within its scope.// - Use with caution due to potential scoping issues.
// - let: (Scope: Block)// - Can be reassigned but not redeclared within its scope.// - Preferred for variables that need to be reassigned.
// - const: (Scope: Block)// - Cannot be reassigned or redeclared.// - Use for values that should remain constant.
// Note:// - Use 'let' for most variable declarations.// - Use 'const' for values that should not change.
cotrVarTyped
TypeScript Create Typed Variable
let ${1:myVar}: ${2:Type} = $3;
cotrGenList
TypeScript Generate Array
const ${1:myList} = Array.from({ length: ${2:length} }, (_, index) => ${3:'item'} + index);
cotrEntry
TypeScript Entry Point
// TypeScript Entry Point// To run this program, use: `ts-node filename.ts`// Note: Ensure you have ts-node installed globally (`npm install -g ts-node`)
console.log('Hello, World!');
cotrNotEqual
TypeScript Not Equal To
!==
cotrTypesNum
TypeScript Number Type
number
cotrNull
TypeScript Null Type
null
cotrForOfLoop
TypeScript For…Of Loop
for (const item of ${1:iterable}) { ${2:// Your code here}}
cotrCommentMulti
TypeScript Multi-Line Comment
/* * ${1:Your comment here} */
cotrClass
TypeScript Class
class ${1:MyClass} { ${2:// Your code here}}
cotrEqual
TypeScript Equal To
===
cotrTypesDate
TypeScript Date Type
Date
cotrTypesMap
TypeScript Map Type
Map<${1:KeyType}, ${2:ValueType}>
cotrVarStringMulti
TypeScript Create Multi-Line String Variable
let ${1:myString}: string = `${2:Line 1}${3:Line 2}${4:Line 3}`;
cotrVarBool
TypeScript Create Boolean Variable
let ${1:myBoolean}: boolean = ${2:true};
cotrIfElse
TypeScript If Else Statement
if (${1:condition}) { ${2:// Your code here}} else if (${3:condition}) { ${4:// Your code here}} else { ${5:// Your code here}}
cotrVarList
TypeScript Create List Variable
let ${1:myList}: ${2:type}[] = [${3:'item1'}, ${4:'item2'}];
cotrPrint
TypeScript Print
console.log(${1:'Your message here'});
cotrFuncArgs
TypeScript Function Args
// In TypeScript, functions can have arguments with default values.function ${2:myFunction}(${3:arg1}: ${4:type1}, ${5:arg2}: ${6:type2} = ${7:defaultValue}): ${1:void} { ${8:// Your code here}}
cotrFuncSyntax
TypeScript Function Syntax
// TypeScript Function Syntax// Basic function: function functionName(parameters): ReturnType { ... }// Function with arguments: function functionName(arg1: Type1, arg2: Type2, ...): ReturnType { ... }// Function with named arguments: function functionName({arg1, arg2, ...}: {arg1: Type1, arg2: Type2, ...}): ReturnType { ... }