-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
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 beforeignoreElements() - 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?