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.

Angular 11 - Documentation - Tutorial: Tour of Heroes property not initialized error from typescript

See original GitHub issue

📚 Docs or angular.io bug report

Description

Tour of heroes tutorial in Angular 11 (typescript version ~4.0.2), when instructed to add “selectedHero”: Hero to the heroes.component, typescript now complains:

Property ‘selectedHero’ has no initializer and is not definitely assigned in the constructor.ts(2564)

🔬 Minimal Reproduction

Follow Tour of heroes documentation

What’s the affected URL?**

https://angular.io/tutorial/toh-pt2

Reproduction Steps**

Follow Tutorial instructions

Expected vs Actual Behavior**

Should not produce error

This can be worked around by changing the line of code to: selectedHero!: Hero; // Note the exclamation mark.

🔥 Exception or Error




import { Component, OnInit } from '@angular/core';

import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.scss'],
})
export class HeroesComponent implements OnInit {
  heroes = HEROES;
  selectedHero: Hero;                                     <--   ERROR occurs here.   fix by changing to selectedHero!: Hero;

  constructor() {}

  ngOnInit(): void {}

  onSelect(hero: Hero) {
    this.selectedHero = hero;
  }
}

🌍 Your Environment

Windows 10 x64

_                      _                 ____ _     ___
/ \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|

/ △ \ | '_ \ / | | | | |/ _ | '__| | | | | | | / ___ | | | | (| | || | | (| | | | || | | | // __| ||_, |_,||_,|| _||| |___/

Angular CLI: 11.0.3 Node: 14.4.0 OS: win32 x64

Angular: 11.0.3 … animations, cli, common, compiler, compiler-cli, core, forms … platform-browser, platform-browser-dynamic, router Ivy Workspace: Yes

Package Version

@angular-devkit/architect 0.1100.3 @angular-devkit/build-angular 0.1100.3 @angular-devkit/core 11.0.3 @angular-devkit/schematics 11.0.3 @schematics/angular 11.0.3 @schematics/update 0.1100.3 rxjs 6.6.3 typescript 4.0.5

Browser info

No.

Anything else relevant?

No

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
DaveSharmancommented, Dec 4, 2020

This appears to be caused by settings by default in the ts.json file

“compileOnSave”: false, “compilerOptions”: { “baseUrl”: “./”, “outDir”: “./dist/out-tsc”, “forceConsistentCasingInFileNames”: true, “strict”: true, << Strict causes this. “noFallthroughCasesInSwitch”: true, “sourceMap”: true, “declaration”: false, “downlevelIteration”: true, “experimentalDecorators”: true, “moduleResolution”: “node”, “importHelpers”: true, “target”: “es2015”, “module”: “es2020”, “lib”: [ “es2018”, “dom” ] }, “angularCompilerOptions”: { “strictInjectionParameters”: true, “strictInputAccessModifiers”: true, “strictTemplates”: true }

vs the tsconfib.json from the demo project downloaded from the Tour of heroes Tutorial. Note the comments around fixing the code, and then enable these options.

/* To learn more about this file see: https://angular.io/config/tsconfig. */ { "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "forceConsistentCasingInFileNames": true, // TODO(gkalpak): Fix the code and enable this. // "strict": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "sourceMap": true, "declaration": false, "downlevelIteration": true, "experimentalDecorators": true, "moduleResolution": "node", "importHelpers": true, "target": "es2015", "module": "es2020", "lib": [ "es2018", "dom" ] }, "angularCompilerOptions": { "strictInjectionParameters": true, "strictInputAccessModifiers": true, // TODO(gkalpak): Fix the code and enable this (i.e. switch fromfullTemplateTypeChecktostrictTemplates`). “fullTemplateTypeCheck”: true,// “strictTemplates”: true } }

`

0reactions
angular-automatic-lock-bot[bot]commented, Jun 3, 2021

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add services - Angular
This tutorial creates a HeroService that all application classes can use to get heroes. Instead of creating that service with the new keyword,...
Read more >
File 'app/hero.ts' is not a module error in the console, where to ...
Try Restarting the editor in which you are writing the code(VS code or Sublime). Compile and Run it again. I have done the...
Read more >
Introduction to Angular: Tour of Heroes - YouTube
Become a Patreon! Your continued support enables me to continue creating free content: https://www.patreon.com/PaulHallidayIntroduction to ...
Read more >
Using an NgRX Store Module in an Angular Application
In this case, the type property is ADD_DEVELOPER , meaning that you are dispatching an action to add the new Developer object stored...
Read more >
Angular TypeScript Tutorial in Visual Studio Code
The Visual Studio Code editor supports TypeScript IntelliSense and code navigation out of the box, so you can do Angular development without installing...
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