Drag0 Drag0 - 1 year ago 64
TypeScript Question

Why isn't Angular2 Component property showing in my page?

This is my code:

import {Component} from "@angular/core";

selector: 'my-app',
template: `

export class AppComponent {
title = 'Lovely Jubbly';
secondTitle: 'Bummer...'

title 'Lovely Jubbly' appears on my site, but secondTitle 'Bummer...' doesn't show. Only difference is how I initialized those variables. I'm TypeScript and Angular2 noob so it might be an obvious mistake. Could anyone direct me to some docs that could explain this behaviour?

Answer Source

TypeScript is interpreting Bummer... as a type (eg, a string that can only be "Bummer..."). When it becomes JavaScript, it will still be initialized to null.

If you're wondering, those types can be useful for certain enum-like variables. eg:

var trafficLightState:'RED' | 'GREEN' | 'YELLOW' = 'RED';
trafficLightState = 'Melon'; // ERROR!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download