Skip to content

TSConfig 'strict: false' leads to an errorΒ #52548

@Gadicuz

Description

@Gadicuz

Bug Report

πŸ”Ž Search Terms

strict generic strictFunctionTypes

πŸ•— Version & Regression Information

I tried TS 4.9.4 and 4.8.4 on Playground.

⏯ Playground Link

Playground link with relevant code

I asked the question on Stackoverflow, but nobody answered,

πŸ’» Code

import { of, tap, ignoreElements } from 'rxjs';
export class C {
  private bar: number[] = [];
  public foo() {
    return of([1]).pipe(
      tap((x) => (this.bar = x)),
      ignoreElements()
    );
  }
}

πŸ™ Actual behavior

The code is fine until you turn off 'strict' mode (to be precise 'strictFunctionTypes'). Uncheck 'strictFunctionTypes' and get errors in line this.bar = x:

Type 'unknown' is not assignable to type 'number[]'.
Type '{}' is missing the following properties from type 'number[]': length, pop, push, concat, and 26 more.

I discovered several ways to remove the errors in 'strict=false' mode:

  • set explicitly type of arrow function argument x (x: number[]) => ...
  • add another 'empty' tap() just before ignoreElements()
  • remove ignoreElements() (removes error, alters behavior)

It seems pipe() signature resolves differently with strict mode on/off.
It's pipe<number[], never>(...) when strict=true
It's pipe<unknown, never>(...) when strict=false
I guess one of the reasons is ignoreElements(): OperatorFunction<unknown, never> signature with non-generic 'unknown'.

So I get error-free code with strict=true but it fails with strict=false.

πŸ™‚ Expected behavior

The strict flag enables a wide range of type checking behavior that results in stronger guarantees of program correctness.

Usually expect more errors in strict mode, but this error appears only if strict mode off.
Is this TS behavior intended? What is the recommended solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions