Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

412
Visualizações
RXJS 'never' subject as the generic type

I am working with RXJS and Typescript in an angular project. I have written the following in my code:

const sub = new Subject<never>();

I expect this line to mean that any subscriber that defines the 'next' and 'error' methods, as can be seen below, will never have the 'next' method called. Only the 'error' method will be called, if at all.

sub.subscribe(() => /*do something with 'next' value*/, error => /*do something with 'error' value*/)

However, when I write the following:

sub.next();

I do not get a compilation error.

From my understanding the Subject generic is the type returned by the subject, so never means that we never call the next method.

Can anyone explain why? Is this a bug, a feature or misunderstanding on my part?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Specifying the type param on your Subject does restrict the type of value that you can pass when you call .next(), so if you attempt to pass a value: sub.next('string value') you will get an error:

Argument of type 'string' is not assignable to parameter of type 'never'.(2345)

However, the value parameter of Subject.next() is optional (in RxJS v6). This means, calling sub.next() without passing argument is allowed, so you can call Subject.next with both an argument of type T or with no argument at all.


I believe this behavior has changed in RxJS v7. Interesting GitHub Issue

over 4 years ago · Santiago Trujillo Relatório

0

It looks like for Subject<T> the next method is defined like this: next(val: T | undefined): void, so you can always pass undefined into it. This is different to BehaviorSubject for example, where the signature is next(val: T): void and trying to call next will produce a compile-time error.

I'm not sure why this is the case.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda