Skip to content

Type guards are not working properly in Observable #19334

Closed
@aminpaks

Description

@aminpaks

TypeScript Version: 2.5.3

Code

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';
import 'rxjs/add/operator/filter';

type AType = 'first' | 'second';

function isFirst(value: AType): value is 'first' {
  return value === 'first';
}

const inputs: AType[] = ['first', 'second'];
const obx = Observable.from(inputs);

obx
  .filter(item => isFirst(item)) // <- checking types with guard
  .subscribe(item => console.log(item)); // <- here item is still `'first' | 'second'`!

A simple example to reproduce.

Expected behavior:
Type guards must make sure the flow continues according to the current status of types.

Actual behavior:
Apparently because the type guard is not within a branch it is not working as intended, or it is working as intended by design that I personally think is not a good idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions