question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subject compiles wrong or do I make a mistake?

See original GitHub issue

I created a simple service. It should send “visible” broadcasts to all subscribers. After compiling it, I got the error message:

ERROR TypeError: Subject_1.Subject is not a constructor at new VisibleService (visible.service.js:24)

But when I add .default to the line this.source = new Subject_1.Subject(); the problems disappears.

RxJS version: rxjs@5.4.3 loaded by systemjs@0.20.18 and compiled by typescript 2.4.2

Code to reproduce:

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class VisibleService {
  private source = new Subject<boolean>();

  onDataChange(data: boolean) {
    this.source.next(data);
  }

  observer = this.source.asObservable();
}

Expected behavior:

    "use strict";
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __moduleName = context_1 && context_1.id;
    var core_1, Subject_1, VisibleService;
    return {
        setters: [
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (Subject_1_1) {
                Subject_1 = Subject_1_1;
            }
        ],
        execute: function () {
            VisibleService = (function () {
                //import { BehaviorSubject } from "rxjs/BehaviorSubject"
                function VisibleService() {
                    this.source = new Subject_1.default.Subject();
                    this.observer = this.source.asObservable();
                }
                //private source = new BehaviorSubject<boolean>(false);
                VisibleService.prototype.onDataChange = function (data) {
                    this.source.next(data);
                };
                VisibleService = __decorate([
                    core_1.Injectable()
                ], VisibleService);
                return VisibleService;
            }());
            exports_1("VisibleService", VisibleService);
        }
    };
});

Actual behavior:

    "use strict";
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __moduleName = context_1 && context_1.id;
    var core_1, Subject_1, VisibleService;
    return {
        setters: [
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (Subject_1_1) {
                Subject_1 = Subject_1_1;
            }
        ],
        execute: function () {
            VisibleService = (function () {
                //import { BehaviorSubject } from "rxjs/BehaviorSubject"
                function VisibleService() {
                    this.source = new Subject_1.Subject();
                    this.observer = this.source.asObservable();
                }
                //private source = new BehaviorSubject<boolean>(false);
                VisibleService.prototype.onDataChange = function (data) {
                    this.source.next(data);
                };
                VisibleService = __decorate([
                    core_1.Injectable()
                ], VisibleService);
                return VisibleService;
            }());
            exports_1("VisibleService", VisibleService);
        }
    };
});

Additional information: My tsconfig.json:

{
    "compilerOptions": {
        "baseUrl": "../js/3rd",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": ["es2017", "dom"],
        "module": "System",
        "outDir": "../js",
        "target": "ES5"
    }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kwonojcommented, Aug 28, 2017

You can rule out by not invloving any angular/systemjs but creating plain ts with rx only to verify it first, by creating subject only as snippet does. If it still doesn’t work, then rx does something wrong. If not, probably some config or behavior of systemjs need to be looked into.

0reactions
lock[bot]commented, Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Art of the “Oops” – Email Mistakes and What to Do About ...
Email mistakes happen when sending out client communications. Here's our newest blog on how to recover successfully from an “oops.”
Read more >
30 Email Mistakes You Don't Want to Make and Tips on How ...
30 Email Mistakes You Don't Want to Make and Tips on How to Do Better · #1. Rushing to reply · #2. Forwarding...
Read more >
THE TOP TEN GRAMMAR MISTAKES PEOPLE MAKE
Microsoft recently compiled a list of the top ten grammar mistakes people make while using the English language. This data was culled from ......
Read more >
Top 10 Grammar Mistakes High School Students Make (and ...
SUBJECT -VERB AGREEMENT ... Subjects and verbs must match. If the noun is plural, the verb must also be plural and vice versa....
Read more >
What To Do After You Make A Mistake At Work (With Examples)
Accept your emotions and feelings and then move on. Okay, so you feel bad about your error. Whatever emotions it brings up, none...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found