Closed cfsamson closed 3 years ago
Yes, this is a known issue. Many examples do not work in strict mode. This is being tracked in #39344.
@gkalpak Oh, I see. I didn't know that option was so new. I made a PR to fix this one here: https://github.com/angular/angular/pull/40942#. I can suggest a fix for the ones I spot as I go through the Tour of Heros tutorial.
I made a PR to fix this one here: #40942
:100:
I can suggest a fix for the ones I spot as I go through the Tour of Heros tutorial.
That would be fantastic! Thank you, @cfsamson π
There's another issue later in the tutorial when you get to https://angular.io/tutorial/toh-pt5#extract-the-id-route-parameter
const id = +this.route.snapshot.paramMap.get('id');
It complains id might be null and there's no fix other than handling it explicitly. On that subject the tutorial is a bit broken when it comes to error handling there. What if id really is not provided? What if it's not a number? What if there is no hero with that id? I really don't know, I'm new here.
You just need to use @Input hero !: Hero
. This will make hero nullable
and won't cause error.
@cfsamson if issue is fixed, can we close this ?
Yes, thanks @iRealNirmal
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.
π Docs or angular.io bug report
Description
The example code provided in Tutorial of Heros, chapter 3, section: Add the Input() hero property causes an error stating that "Property 'hero' has no initializer and is not definitely assigned in the constructor".
This might be caused by specifying "strict" mode when generating the app. Probably related to: https://github.com/angular/angular/issues/39966 (which seems to be fixed now)
It's caused by adding the line:
Specifying that
hero
is an optional property fixes the problem:π¬ Minimal Reproduction
Follow the tutorial choosing strict when asked by
ng new
or by setting"strict": true
in tsconfig.json:What's the affected URL?**
https://angular.io/tutorial/toh-pt3#add-the-input-hero-property
Expected vs Actual Behavior**
Expected the tutorial to work, even though setting the ts transpiler to strict mode.
π₯ Exception or Error
π Your Environment
Browser info
The error is not browser specific.
Anything else relevant?
When asked by
ng new
if you want to enable strict mode, a lot of readers will choose "yes" since type checking and correctness it's part of what TypeScript promises to improve. Fixing the error requires some prior knowledge that tutorial readers might not have.