-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update HTTP guide #32045
Update HTTP guide #32045
Conversation
You can preview 093c698 at https://pr32045-093c698.ngbuilds.io/. |
093c698
to
81e59d9
Compare
You can preview 81e59d9 at https://pr32045-81e59d9.ngbuilds.io/. |
aio/content/guide/http.md
Outdated
You can structure your `HttpClient` request to include the type of the response object, to make consuming the output easier and more obvious. | ||
Specifying the response type acts as a type assertion during the compile time. | ||
|
||
For example, the subscribe callback above requires bracket notation to extract the data values. | ||
|
||
<code-example | ||
path="http/src/app/config/config.component.ts" | ||
region="v1_callback"> | ||
</code-example> | ||
|
||
You can't write `data.heroesUrl` because TypeScript correctly complains that the `data` object from the service does not have a `heroesUrl` property. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't true - in the example the type is declared in the callback function, so TypeScript knows that data
is of type Config
and data.heroesUrl
is valid to write. I don't know any circumstance in TypeScript where dot-property access isn't allowed but bracket access is. Under some configurations they can mean different things to the optimizer, but they both should be allowed.
I think you're looking for an example of the form:
.subscribe(data => this.config = {
heroesUrl: (data as any).heroesUrl,
textfile: (data as any).textfile,
});
Here, data
is of type Object
, so accessing data.heroesUrl
or data.textfile
isn't allowed without the cast.
It's possible to specify the type in the callback function as done in the example currently, but that only works for that specific subscribe
callback function - any RxJS operators, etc will also need the type specified. The advantage of specifying it as a type parameter to .get
is that the entire Observable
will be correctly typed, no matter where you store/pass it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restructured these sections - please check.
aio/content/guide/http.md
Outdated
|
||
<code-example | ||
path="http/src/app/config/config.service.ts" | ||
region="getConfig_2" | ||
header="app/config/config.service.ts (getConfig v.2)"> | ||
</code-example> | ||
|
||
<div class="alert is-important"> | ||
|
||
When you pass an interface or a class as a type parameter to the `HttpClient.get()` method, use the RxJS `map` operator to read the response data. Angular parses the response with `JSON.parse()`, but does not instantiate an object of the type parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you pass an interface or a class as a type parameter to the `HttpClient.get()` method, use the RxJS `map` operator to read the response data. Angular parses the response with `JSON.parse()`, but does not instantiate an object of the type parameter. | |
When you pass an interface as a type parameter to the `HttpClient.get()` method, use the RxJS `map` operator to read the response data. Angular parses the response with `JSON.parse()`, but does not instantiate an object of the type parameter. |
(changed to not mention classes)
Also, I think this section is misleading - map
should be used to transform data, it has no special connection with type parameters. The role of map
is to convert from the model the server returns to the model needed by the UI, so that subscribing can be done with the async
pipe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check
81e59d9
to
5a3821d
Compare
You can preview 5a3821d at https://pr32045-5a3821d.ngbuilds.io/. |
You can preview 853a538 at https://pr32045-853a538.ngbuilds.io/. |
853a538
to
ad9916f
Compare
You can preview ad9916f at https://pr32045-ad9916f.ngbuilds.io/. |
45a718f
to
662ec0f
Compare
You can preview 662ec0f at https://pr32045-662ec0f.ngbuilds.io/. |
662ec0f
to
041d9b7
Compare
You can preview 041d9b7 at https://pr32045-041d9b7.ngbuilds.io/. |
041d9b7
to
e965f68
Compare
You can preview e965f68 at https://pr32045-e965f68.ngbuilds.io/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make the suggested adjustments, otherwise lgtm. thanks!
d251fa6
to
7cfe0ca
Compare
You can preview d251fa6 at https://pr32045-d251fa6.ngbuilds.io/. |
You can preview 7cfe0ca at https://pr32045-7cfe0ca.ngbuilds.io/. |
You can preview 37da89e at https://pr32045-37da89e.ngbuilds.io/. |
aio/content/guide/http.md
Outdated
|
||
The `HttpClient.get()` method parsed the JSON server response into the anonymous `Object` type. It doesn't know what the shape of that object is. | ||
Notice that in the above example, the `subscribe` callback uses bracket notation to extract the data values. | ||
You can't write `data.heroesUrl` because TypeScript correctly complains that the data object from the service does not have a `heroesUrl` property. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still not true - data['heroesUrl'] and
data.heroesUrl` are the same in TypeScript, with the same type-checking rules applied. There isn't a circumstance where the former is valid and the latter isn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just removing the whole paragraph, I hope that works.
You can preview 2a8c292 at https://pr32045-2a8c292.ngbuilds.io/. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
HTTP guide needs work -- long and confusing
Issue Number: N/A
What is the new behavior?
Edit and reorganize content - from unfinished PR #28519
Does this PR introduce a breaking change?