ESLint reports an invalid type in a template literal in TypeScript
Invalid type "any" of template literal expressionShort answer
The rule restrict-template-expressions limits what can go inside a template string. The strict configs turn off allowAny, allowNumber, allowBoolean and allowNullish. Give the value a real type, convert it with String() or a fallback, or turn on the specific allow option if you accept that risk.
Fix it fast
1Find out which cause you have
Find the line the rule points at and hover the expression in your editor to see its type.
npx eslint src --rule '{"@typescript-eslint/restrict-template-expressions": "error"}'2Match what you see, then jump to its fix
- 1
The message names type "any"The value is typed anyFix - 2
Strict or strict-type-checked is extended and the value is a number, boolean, null or undefinedA number, boolean or nullable value in a strict configFix - 3
The type named in the message is an object or arrayAn object or array inside the templateFix
- 1
What does the error look like?
Invalid type "any" of template literal expression@typescript-eslint/restrict-template-expressions
Cause 1 of 3
The value is typed any
1Check
JSON responses, catch variables and untyped props are often any. With allowAny false, any is rejected.
grep -rn "as any\|: any" src/2Fix
Type the value, or parse it into a typed object first.
type User = { name: string };
const user: User = await res.json();
const label = `Hello ${user.name}`;3Verify
ESLint no longer reports the line.
npx eslint srcConfirmed by typescript-eslint.io
Cause 2 of 3
A number, boolean or nullable value in a strict config
1Check
Those configs set allowNumber, allowBoolean and allowNullish to false, unlike the recommended config.
grep -n "strict" eslint.config.*2Fix
Convert explicitly, or give a fallback for a nullable value.
const a = `Total: ${String(count)}`;
const b = `Name: ${name ?? "unknown"}`;3Verify
Run ESLint on the file.
npx eslint src/Cart.tsxConfirmed by typescript-eslint.io
Cause 3 of 3
An object or array inside the template
1Check
Objects would print as [object Object], and arrays are disallowed by default.
npx eslint src/List.tsx2Fix
Print the property you need, or format an array with Intl.ListFormat.
const names = new Intl.ListFormat("en").format(users.map(u => u.name));
const text = `Members: ${names}`;3Verify
ESLint passes and the string reads correctly.
npx eslint src/List.tsxConfirmed by typescript-eslint.io
How do I stop it coming back?
- Avoid any at the source: type API responses and event handlers.
- Choose the strict or recommended config once and keep the rule options in version control.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix