Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The string type in TypeScript is a primitive data type representing an immutable sequence of UTF-16 code units. It encompasses all textual data and serves as the foundational type for both standard text assignment and advanced, string-based type transformations within the TypeScript compiler. TypeScript supports three syntax forms for string initialization, identical to JavaScript: double quotes ("), single quotes ('), and backticks (`) for template literals.
const double: string = "text";
const single: string = 'text';
const interpolated: string = `value: ${double}`;

String Literal Types

Beyond the generic string primitive, TypeScript allows exact string values to be declared as discrete types. A string literal type restricts a variable to one specific sequence of characters.
let exactValue: "SUCCESS";
exactValue = "SUCCESS"; // Valid
exactValue = "PENDING"; // Type Error: Type '"PENDING"' is not assignable to type '"SUCCESS"'.
Literal types are frequently combined using union operators to create finite sets of permissible string values.
type Direction = "NORTH" | "SOUTH" | "EAST" | "WEST";
let heading: Direction = "NORTH";

Template Literal Types

TypeScript extends JavaScript’s template literal syntax into the type system. Template literal types allow you to build new string types by concatenating or interpolating existing string literal types. When interpolating unions, TypeScript computes the cross-product of all possible combinations.
type Color = "Red" | "Blue";
type Size = "Small" | "Large";

type ProductVariant = `${Size}-${Color}`;
// Resolves to: "Small-Red" | "Small-Blue" | "Large-Red" | "Large-Blue"

Intrinsic String Manipulation Types

The TypeScript compiler provides four intrinsic utility types specifically for transforming string literal types at the type level. These are implemented directly in the compiler for performance.
type Base = "helloWorld";

type U = Uppercase<Base>;    // "HELLOWORLD"
type L = Lowercase<Base>;    // "helloworld"
type C = Capitalize<Base>;   // "HelloWorld"
type UC = Uncapitalize<Base> // "helloWorld"
These utilities can be combined with template literal types to enforce strict naming conventions dynamically.
type Entity = "user" | "post";
type FetcherMethod = `get${Capitalize<Entity>}`;
// Resolves to: "getUser" | "getPost"

string vs. String

TypeScript distinguishes between the primitive string type and the global String wrapper object. The lowercase string represents the primitive value and should be used for type annotations. The uppercase String refers to the JavaScript object prototype and should generally be avoided in type signatures.
const primitiveString: string = "hello"; // Correct
const objectString: String = new String("hello"); // Valid, but anti-pattern for typing

// Type Error: 'string' is a primitive, but 'String' is a wrapper object.
const mismatched: string = new String("hello"); 
Master TypeScript with Deep Grasping Methodology!Learn More