Answer the question
In order to leave comments, you need to log in
Why does typescript compiler swear?
All the best!
So I decided to get acquainted with Angular 2 (+ typescript with es6 at the same time).
Just digging in for now... here is my code snippet :
export class AppComponent {
title = 'hero';
hero: Hero = {
id: 1,
name: 'Wind'
}
}
tsc && concurrently \"tsc -w\" \"lite-server\"
export class AppComponent {
constructor (){
this.sommm = 'from this some'
}
title = 'hero';
hero: Hero = {
id: 1,
name: 'Wind'
}
}
constructor (){
this.sommm = 'from this some'
}
tsc && concurrently "tsc -w" "lite-server"
app/app.component.ts(15,10): error TS2339: Property 'sommm' does not exist on type 'AppComponent'.
npm ERR! Darwin 15.5.0
npm ERR! argv "/Users/scorpio/.nvm/versions/node/v5.1.0/bin/node" "/Users/scorpio/.nvm/versions/node/v5.1.0/bin/npm" "start"
npm ERR! node v5.1.0
npm ERR! npm v3.3.12
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `tsc && concurrently "tsc -w" "lite-server" `
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] start script 'tsc && concurrently "tsc -w" "lite-server" '.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the angular2-quickstart package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! tsc && concurrently "tsc -w" "lite-server"
npm ERR! You can get their info via:
npm ERR! npm owner ls angular2-quickstart
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/scorpio/Documents/git/io2/heroes/npm-debug.log
Answer the question
In order to leave comments, you need to log in
I’ll correct Evgeny a little, it’s enough to declare the desired field in the class, the type can be omitted:
export class AppComponent {
sommm; // Можно и без типа, зависит от настройки компилятора
title = 'hero';
hero: Hero = {
id: 1,
name: 'Wind'
}
constructor (){
this.sommm = 'from this some'
}
}
export class AppComponent {
// В Angular 2 так можно делать в классах,
// которые не являются компонентами/директивами/сервисами/пайпами и т.п.
constructor (public sommm: string = 'from this some') {}
}
// Это аналогично такому:
export class AppComponent {
sommm: string;
constructor (sommm: string = 'from this some') {
this.sommm = sommm;
}
}
let a;
function foo(bar) {}
class Bar {
foo;
private bar;
}
Try declaring the type of this variable before the constructor
. Your error says that there is no such property in the class:
"Property 'sommm' does not exist on type 'AppComponent'."
sommm: string;
constructor() {
this.sommm = 'from this some';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question