Skip to content

Filter operator incorrect type inference #2927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Iorg opened this issue Oct 9, 2017 · 4 comments
Closed

Filter operator incorrect type inference #2927

Iorg opened this issue Oct 9, 2017 · 4 comments

Comments

@Iorg
Copy link

Iorg commented Oct 9, 2017

RxJS version: 5.5.0-beta.5

Code to reproduce:

const a = Observable.from([1, '1'])
    .pipe(
        filter((x): x is string => typeof x === 'string')
    );

const b = Observable.from([1, '1'])
        .filter((x): x is string => typeof x === 'string');

Expected behavior:
Type of a and b must be Observable<string>

Actual behavior:
Type of a and b inferred as Observable<string | number>

Additional information:
Typescript: 2.5.3

tsconfig.json:

{
    "compileOnSave": false,
    "compilerOptions": {
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es5",
        "typeRoots": ["node_modules/@types"],
        "lib": ["es2016", "dom", "es2017.object"],
        "strictNullChecks": true,
        "noImplicitAny": true,
        "noImplicitThis": true,
        "noImplicitReturns": true,
        "strictFunctionTypes": true,
        "plugins": [
            {
                "name": "@angular/language-service"
            }
        ]
    }
}

Hi folks,

You created the most awesome reactive library I ever used. But I faced with the issue I cannot explain to myself.

PS: Are there any additional information I can collect to help with this issue?

@cartant
Copy link
Collaborator

cartant commented Oct 9, 2017

This looks like a TypeScript problem, to me. And not a problem with the RxJS typings.

Interestingly, if x: any is specified in the arrow function passed to filter, the types will be inferred as you are expecting:

const a = Observable.from([1, '1'])
    .pipe(
        filter((x: any): x is string => typeof x === 'string')
    );

const b = Observable.from([1, '1'])
        .filter((x: any): x is string => typeof x === 'string');

They are also inferred as you'd expect if x: number | string is specified.

@Iorg
Copy link
Author

Iorg commented Oct 9, 2017

@cartant Thanks for answering,
It seems you are right here and this is a typescript problem. So we can close this issue.

BTW, did you faced with some relevant issue on TS repo?

@Iorg
Copy link
Author

Iorg commented Oct 9, 2017

I guess, that one: microsoft/TypeScript#18562

@cartant
Copy link
Collaborator

cartant commented Jan 27, 2019

Closing this as it's a TypeScript issue - see the comment above.

@cartant cartant closed this as completed Jan 27, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants