DauleDK
DauleDK

Reputation: 3423

Subscription to rxjs subject get's the wrong type in typescript?

My goal is to open a subscription to an rxjs subject, with the type string | undefined.

My code looks like this:

import {Subject, Observable} from 'rxjs';

const a = new Subject<string | undefined>();

// v has type "string", and not "string | undefined"
a.subscribe(v => console.log(v));

Image that shows the problem

How can I get v to have the type string | undefined?

Here is a stackblitz if you wan't to experiment.

Upvotes: 3

Views: 66

Answers (1)

HTN
HTN

Reputation: 3594

You must have strictNullChecks compiler option enabled: http://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options

Upvotes: 4

Related Questions