Typescript string

Created By: chatGPT

TypeScript is a superset of JavaScript that adds static typing to the language. This allows for better tooling, such as autocompletion and type checking, which can reduce errors during development. You can define types for variables, function parameters, and return values, enhancing code maintainability and readability.
let name: string = 'John';
let age: number = 30;
function greet(person: string): string {
    return `Hello, ${person}!`;
}
console.log(greet(name));
In TypeScript, you can create string types that enforce the kind of data you expect. For example, while declaring a string, you can use literal types to restrict the string value to specific fixed values.
type Greeting = 'Hello' | 'Hi';
let greetMe: Greeting;
greetMe = 'Hello'; // Valid
// greetMe = 'Hey'; // Error: Type 'Hey' is not assignable to type 'Greeting'.
Using template literals in TypeScript allows you to create multi-line strings and include expressions using ${} syntax. This feature improves string manipulation and makes your code cleaner.
let firstName: string = 'Jane';
let lastName: string = 'Doe';
let fullName: string = `${firstName} ${lastName}`;
console.log(fullName); // Outputs: Jane Doe
TypeScript also offers methods that work with strings, such as .slice(), .substring(), and .includes(). These methods help in managing text efficiently.
let message: string = 'TypeScript is great!';
let subMessage: string = message.slice(0, 10);
let containsGreat: boolean = message.includes('great');
console.log(subMessage); // Outputs: TypeScript
console.log(containsGreat); // Outputs: true
You can also define arrays of strings or any other data type in TypeScript, providing more flexibility and organization to your data structures.
let fruits: string[] = ['apple', 'banana', 'cherry'];
fruits.push('date');
console.log(fruits); // Outputs: ['apple', 'banana', 'cherry', 'date']
Introduction And SetupVariablesData TypesAnyUnknownVoidNeverStringNumberBooleanArrayTupleEnumObjectInterfaceType AliasMethodsFunctionArrow FunctionReturn TypeParametersDefault ParametersOptional ParametersRest ParametersControl FlowIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor...of LoopFor...in LoopBreak StatementContinue StatementFunctionsFunction OverloadingRecursive FunctionExpression FunctionOperatorsArithmetic OperatorsAssignment OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsConditional (ternary) OperatorData StructuresArrayTupleObjectMapSetCommentsSingle Line CommentMulti Line CommentSyntaxType AnnotationsType InferenceNamespaceModuleExportImportDecoratorsAmbient DeclarationsEvent HandlingEvent ListenersAddEventListenerRemoveEventListenerError HandlingTry...catch StatementThrow StatementFinally BlockCustom Error TypesAsync AwaitPromisesGenericsConditional TypesMapped TypesIntersection TypesUnion TypesLiteral TypesDiscriminated UnionsType GuardsTypeof GuardInstanceof GuardAssertion FunctionsModule Augmentation