A TypeScript string is a JavaScript string with the type string. For interpolation, write a template literal: backticks with ${expression} placeholders. Single and double quotes make plain strings with no interpolation.
All three quote styles produce the same type, string. The first $ in $${...} is a literal dollar sign; only ${ starts a placeholder.
String Interpolation
Inside ${ } you can put a variable, arithmetic, a function call, a ternary or a nested template literal. The value is converted to a string the same way String(value) would convert it (a symbol is the exception: interpolating one is compile error TS2731, and it would throw a TypeError at runtime; write String(sym)).
Interpolating an object prints [object Object], which is almost never what you want. Pick the fields, or use JSON.stringify(obj).
Multiline Strings
A template literal can span lines. The newlines are kept, and so is any indentation at the start of each line, because everything between the backticks is part of the string.
When the indentation of your code would leak into the string, build it with an array and join("\n"), or keep the template literal's lines flush with the start of the line.
Checking if a String Contains a Substring
includes answers "does it contain" with a boolean. startsWith and endsWith check the ends. indexOf gives the position, or -1 when the substring is absent. All of them are case-sensitive.
includes also accepts a start position: file.includes("R", 1) only searches from index 1.
Common Methods and Their Types
TypeScript knows the return type of every built-in method. The ones that can come back empty are the ones to watch, because under strict you must handle null or undefined before using the result.
| Method | Returns | Example |
|---|---|---|
includes, startsWith, endsWith | boolean | "abc".includes("b") is true |
indexOf, lastIndexOf | number (-1 if absent) | "abc".indexOf("c") is 2 |
slice, substring | string | "hello".slice(1, 3) is "el" |
split | string[] | "a,b".split(",") is ["a", "b"] |
trim, trimStart, trimEnd | string | " x ".trim() is "x" |
toUpperCase, toLowerCase | string | "Ts".toUpperCase() is "TS" |
padStart, padEnd | string | "7".padStart(3, "0") is "007" |
replace, replaceAll | string | "a-b-c".replaceAll("-", "") is "abc" |
repeat | string | "ab".repeat(2) is "abab" |
at | string | undefined | "abc".at(-1) is "c" |
match | RegExpMatchArray | null | "a1".match(/\d/) |
length (property) | number | "abc".length is 3 |
Plain indexing, s[5], has type string even when the index is past the end (where the value is undefined at runtime). The compiler option noUncheckedIndexedAccess makes it string | undefined instead.
Strings Are Immutable
You cannot change a character in place. TypeScript reports assignment to an index as error TS2542, Index signature in type 'String' only permits reading., and in the strict-mode JavaScript it compiles to, the same assignment would throw a TypeError at runtime. Every method that seems to modify a string returns a new one.
To turn other values into strings, or strings into numbers, see string to number.
String Literal Types
string accepts any text. A string literal type accepts one exact value, and a union of them makes an enum-like set of allowed strings:
The literal types are checked only at compile time; at runtime label("deleted") would still run and print Post is deleted, which is what the second line does here. Template literal syntax also works at the type level, as in type EventName = `on${string}`, which accepts any string that starts with on.
Frequently Asked Questions
How do you do string interpolation in TypeScript?
Use a template literal: backticks with ${expression} placeholders, as in `Hello, ${name}!`. Any expression works inside the braces, and the result has type string. Single and double quotes do not interpolate.
How do I write a multiline string in TypeScript?
Put the text in backticks and break the lines in the source: a template literal keeps the newlines and also the leading indentation of each line. Alternatively join an array of lines with .join("\n"), or use \n inside an ordinary string.
How do I check if a string contains a substring in TypeScript?
Call text.includes("part"), which returns a boolean. It is case-sensitive, so compare lowercased strings for a case-insensitive check: text.toLowerCase().includes(part.toLowerCase()). startsWith and endsWith check the ends, and indexOf returns the position or -1.
Is there a difference between strings in TypeScript and JavaScript?
No, at runtime they are the same JavaScript strings with the same methods. TypeScript adds the string type, checks that you call methods that exist, and knows the return types, for example that match can return null and at can return undefined.
Can you change a character in a TypeScript string?
No. Strings are immutable, and s[0] = "H" is compile error TS2542 (Index signature in type 'String' only permits reading). Build a new string instead: "H" + s.slice(1).