Why does TypeScript say a defined type is any when compiling

Discussão em 'Angular' iniciado por Chris Barr, Fevereiro 24, 2025.

  1. Chris Barr

    Chris Barr Guest

    I am using using Angular 19, RxJS 7.8.2, and TypeScript 5.7.3 with the following options in my tsconfig.json file:

    {
    "compileOnSave": false,
    "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "lib": ["ES2022", "dom"],
    "forceConsistentCasingInFileNames": true,

    "declaration": false,
    "experimentalDecorators": true,
    "importHelpers": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "useDefineForClassFields": false,

    "strict": true,
    "allowUnreachableCode": false,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true,
    "strictNullChecks": false,
    "strictPropertyInitialization": false
    }
    }


    I recently got this build error which says there is an implicit any type, but in the same error is also shows that the implicit type is the correct type. I am pretty confused by this. For now I am using a //@ts-expect-error comment which allows it to compile just fine, but that feels wrong.

    [​IMG]

    The code is basically set up so that a method returns an observable, and then I pipe the value through to something else, like this, where createDocument$() would return a type of Observable<IExtDocument>:

    return this.someService.createDocument$(contract).pipe(
    map((documentResponse) => { //<-- Error happens here
    // Do stuff in here with the documentResponse
    }
    );


    Even if I try and manually add the type definition with documentResponse: IExtDocument I get the following compilation error, which again shows the type but insists that it's actually any instead:

    Application bundle generation failed. [5.222 seconds]

    X [ERROR] TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. [plugin angular-compiler]

    src/app/path-redacted.component.ts:587:16:
    587 │ map((documentResponse: IExtDocument) => {
    ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Continue reading...

Compartilhe esta Página