Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tour of Heros tutorial causes error in ch 3 when TS transpiler is set to "strict" #40941

Closed
cfsamson opened this issue Feb 22, 2021 · 8 comments
Labels
help wanted An issue that is suitable for a community contributor (based on its complexity/scope). P2 The issue is important to a large percentage of users, with a workaround
Milestone

Comments

@cfsamson
Copy link
Contributor

cfsamson commented Feb 22, 2021

馃摎 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: #39966 (which seems to be fixed now)

It's caused by adding the line:

@Input() hero: Hero;

Specifying that hero is an optional property fixes the problem:

@Input() hero?: Hero;

馃敩 Minimal Reproduction

Follow the tutorial choosing strict when asked by ng new or by setting "strict": true in tsconfig.json:

{
  ...
  "compilerOptions": {
    "strict": true,
    ...
  }
}

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




Failed to compile.

src/app/hero-detail/hero-detail.component.ts:12:14 - error TS2564: Property 'hero' has no initializer and is not definitely assigned in the constructor.

12     @Input() hero: Hero;

馃實 Your Environment

ngular CLI: 11.2.1
Node: 14.15.4
OS: win32 x64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1102.1
@angular-devkit/build-angular   0.1102.1
@angular-devkit/core            11.2.1
@angular-devkit/schematics      11.2.1
@schematics/angular             11.2.1
@schematics/update              0.1102.1
rxjs                            6.6.3
typescript                      4.1.5

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.

cfsamson added a commit to cfsamson/angular that referenced this issue Feb 22, 2021
Not explicitly setting the `hero` property as an optional property causes a compilation error: `Property 'hero' has no initializer and is not definitely assigned in the constructor` when having the ts transpiler set to "strict" mode.
cfsamson added a commit to cfsamson/angular that referenced this issue Feb 22, 2021
docs: fix issue angular#40941

<body>

setting the `hero` property as an optional property fixes the compilation error: `Property 'hero' has no initializer and is not definitely assigned in the constructor` when having the ts transpiler set to "strict" mode.
cfsamson added a commit to cfsamson/angular that referenced this issue Feb 22, 2021
setting the `hero` property as an optional property fixes the compilation error: `Property 'hero' has no initializer and is not definitely assigned in the constructor` when having the ts transpiler set to "strict" mode.
@cfsamson cfsamson mentioned this issue Feb 22, 2021
14 tasks
@gkalpak
Copy link
Member

gkalpak commented Feb 22, 2021

Yes, this is a known issue. Many examples do not work in strict mode. This is being tracked in #39344.

@ngbot ngbot bot added this to the needsTriage milestone Feb 22, 2021
@atscott atscott added help wanted An issue that is suitable for a community contributor (based on its complexity/scope). P2 The issue is important to a large percentage of users, with a workaround labels Feb 22, 2021
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Feb 22, 2021
@cfsamson
Copy link
Contributor Author

cfsamson commented Feb 22, 2021

@gkalpak Oh, I see. I didn't know that option was so new. I made a PR to fix this one here: #40942. I can suggest a fix for the ones I spot as I go through the Tour of Heros tutorial.

cfsamson added a commit to cfsamson/angular that referenced this issue Feb 22, 2021
setting the `hero` property as an optional property fixes the compilation
error: `Property 'hero' has no initializer and is not definitely assigned
in the constructor` when having the ts transpiler set to "strict" mode.
@gkalpak
Copy link
Member

gkalpak commented Feb 23, 2021

I made a PR to fix this one here: #40942

馃挴

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 馃殌

@rinu
Copy link

rinu commented Mar 4, 2021

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.

@hembhagat99
Copy link

You just need to use @Input hero !: Hero. This will make hero nullable and won't cause error.

AndrewKushnir pushed a commit that referenced this issue Mar 5, 2021
setting the `hero` property as an optional property fixes the compilation
error: `Property 'hero' has no initializer and is not definitely assigned
in the constructor` when having the ts transpiler set to "strict" mode.

PR Close #40942
AndrewKushnir pushed a commit that referenced this issue Mar 5, 2021
setting the `hero` property as an optional property fixes the compilation
error: `Property 'hero' has no initializer and is not definitely assigned
in the constructor` when having the ts transpiler set to "strict" mode.

PR Close #40942
@iRealNirmal
Copy link
Contributor

@cfsamson if issue is fixed, can we close this ?

@petebacondarwin
Copy link
Member

Yes, thanks @iRealNirmal

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted An issue that is suitable for a community contributor (based on its complexity/scope). P2 The issue is important to a large percentage of users, with a workaround
Projects
None yet
Development

No branches or pull requests

7 participants