string String Type
Last updated: 2026-10-22
string String Type
string represents text. Use it for names, messages, IDs — any data that lives as characters. It is the most common type in everyday code.
| Group | Basic Types |
|---|---|
| Kind | Type |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
let title: string = "TypeScript Manual";
let greeting = `Hello, ${title}`; // template literals embed variables
⚙️ Parameters
| `${expr}` | Template literal: backtick string with expressions embedded inside ${} |
|---|
Returns:None — string is a basic type annotation and produces no runtime code.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
Item TS-1001 x3
15 ITEM TS-1001 X3
(Runtime output after transpilation; assigning a number to label makes .toUpperCase a compile error)
💡 Notes
ℹ️ Info Always annotate with lowercase string, never the wrapper type String — they differ: String also allows objects like new String().
💡 Tip Prefer template literals over + concatenation — clearer, and they support multi-line text.
⚠️ Note string only guarantees "is text", not the format; validating emails or phone numbers requires runtime regex — types cannot help there.
🏷️ Related Basic Types
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q How do string and String differ?
A string is the primitive type; String is the wrapper object type (also accepts new String() objects). Always annotate with lowercase string.
Q Template literals vs + concatenation?
A Same result, but templates read better, span multiple lines, and ${} accepts any expression. Prefer them for longer strings.
Q How do literal unions like "a" | "b" relate to string?
A string is the superset; a literal union is a subset. A string is not assignable to "a" | "b", but the reverse works. Use literal unions to restrict values (see literal-types).