-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Eliminate the need to cast from Object
to JsonObject
#23567
Comments
Tagging @ampproject/wg-runtime @ampproject/wg-infra @ampproject/wg-performance for visibility. |
Object
to JsonObject
Object
to JsonObject
@jridgewell @erwinmombay Do you know of a way to provide an implicit conversion from For example, this code will currently fail /**
* @return {!JsonObject}
*/
foo() {
return Object.assign({}, {'foo': 'bar'});
} ERROR - [JSC_TYPE_MISMATCH] inconsistent return type
found : Object
required: JsonObject
return Object.assign({}, {'foo': 'bar'});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ However, adding a cast fixes it: /**
* @return {!JsonObject}
*/
foo() {
return /** @type {!JsonObject} */ (Object.assign({}, {'foo': 'bar'}));
} |
Cast is the only way. |
Agree with @jridgewell, these are cases where casting makes sense. |
we also have
|
@jeffkaufman, you recently pointed out casts that seemed unnecessary in ads code. Would |
Stepping back a bit, are we sure we want to use IMO we should extern structs whose shape we definitively know. And only use |
Good question. The
Today, we have about ~1000 instances of I believe @erwinmombay is working on a way to automatically extract known structs so they can be declared in an externs file and used in place of |
Assigning to @erwinmombay, who is dealing with this in #24453 |
@erwinmombay Looks like #24453 has been closed as stale without merging it. Is this still a worthwhile undertaking? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. |
@rileyajones you've been doing a lot of typing-related work recently, does this issue still make sense? |
I think this issue no longer warrants a ticket as the move to TS typing should resolve the underlying issue |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. |
This came out of the discussions at #23559 (comment) and #23559 (comment)
The AMP codebase contains hundreds of casts from
Object
toJsonObject
.I wonder if there's a way to modify the declaration of
JsonObject
inamp.extern.js
to make most of the casts unnecessaryamphtml/build-system/amp.extern.js
Lines 57 to 65 in 6b86000
For converting to
JsonObject
, there isparseJson()
fromsrc/json.js
, but it doesn't work for cases where a non-nullJsonObject
is expectedamphtml/src/json.js
Lines 104 to 113 in c4a663d
For checking if a variable is an
Object
, there isisObject()
fromsrc/types.js
, but it doesn't work when aJsonObject
is expectedamphtml/src/types.js
Lines 48 to 55 in c4a663d
Related to #23425
The text was updated successfully, but these errors were encountered: