TS1271
TypeScriptERRORNotableSyntaxHIGH confidence

Decorator function return type is 'X' but is expected to be 'void' or 'any'.

Production Risk

Build will fail; resolve before shipping.

What this means

A syntax error (TS1271): Decorator function return type is 'X' but is expected to be 'void' or 'any'.. This diagnostic is emitted by the TypeScript compiler when decorator function return type is 'X' but is expected to be 'void' or 'any'..

Why it happens
  1. 1A decorator is applied incorrectly or to an unsupported target
  2. 2The 'experimentalDecorators' flag may need to be enabled
How to reproduce

TypeScript compiler reports TS1271 during type checking.

trigger — this will error
trigger — this will error
// Triggers TS1271
// Decorator function return type is 'X' but is expected to be 'void' or 'any'.

expected output

error TS1271: Decorator function return type is 'X' but is expected to be 'void' or 'any'.

Fix

Enable experimentalDecorators

WHEN Using legacy decorator syntax

Enable experimentalDecorators
// tsconfig.json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Why this works

Decorators require the 'experimentalDecorators' flag; TypeScript 5.0+ also supports the TC39 Stage 3 decorator proposal without the flag.

What not to do

Suppress with @ts-ignore instead of fixing the type

ts-ignore hides real type errors and makes refactoring unsafe.

Sources
Official documentation ↗

TypeScript Compiler Diagnostics

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors