Subject.prototype.next() should only accept T, not T | undefined
See original GitHub issueRxJS version: 5.4.3
Code to reproduce:
const subject = new Subject<string>()
subject.next()
Expected behavior:
Compile error
Actual behavior:
Compiles
Additional information:
next()
's argument is typed as optional, which means it allows undefined
even if the Subject’s type does not. It should only allow T
, not T | undefined
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:23 (16 by maintainers)
Top Results From Across the Web
Generator.prototype.next() - JavaScript - MDN Web Docs
Generator.prototype.next(). The next() method returns an object with two properties done and value . You can also provide a parameter to the next...
Read more >How to Avoid the Infamous "Cannot read properties of ... - Bitovi
That error message is telling you the function is returning undefined implicitly, but its return type does not include undefined in it. Awesome!...
Read more >How does JavaScript .prototype work? - Stack Overflow
In JavaScript you first create an object (there is no concept of class), ... While as said I can't call setAmountDue(), getAmountDue() on...
Read more >Class inheritance - The Modern JavaScript Tutorial
The task may seem simple, but it isn't. The engine knows the current object this , so it could get the parent method...
Read more >Google TypeScript Style Guide
Type parameters, like in Array<T> , may use a single upper case character ( T ) or UpperCamelCase ... The bean that will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The current situation in v6.x is still very unexpected to me as a TypeScript developer when using the
--strict
tsc compiler option (or the --strictNullChecks option to be specific):rxjs Subjects will use the generic type argument and make it nullable:
string[] | undefined
in this case which is somewhat counterintuitive. If I wanted to allowundefined
, I would have defined the type asSubject<string[] | undefined>
. These explicit defines are common and wanted if I turn onstrictNullChecks
.The fact that I always have to pass
undefined
when I create aSubject<void>
is not surprising, but standard when using TypeScript instrict
mode and should be known to the developer:I don’t think rxjs should do some “magic” conversion of
T
toT | undefined
as me as a developer would not expect this. I ran into a bug where I had a.next()
in the code, and the subscriber function had a default argument initializer - so it worked at first. In a refactor I removed the default argument in the subscriber function and the tsc compiler did not report an error, but turned into a runtime error. The whole idea ofstrictNullChecks
is to avoid this.Even worse is that the typing of the
subscribe()
function is wrong: It is infered asT
(and displayed as T in the IntelliSense) but should actually beT | undefined
- the tsc compiler will then enforce the non-null/non-undefined checks inside the subscriber function and we are back to type safety:Please reconsider changing this. This change affects only users which enable strict nullchecks, that option is off by default and when off still allows me to call .next() without passing
undefined
.Closing this because in v7
Subject
has anext
that looks like this:https://github.com/ReactiveX/rxjs/blob/4805a7fecfa5d9732e0329fb463e11d62c219238/src/internal/Subject.ts#L54-L62