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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit $type property in code gen #2956

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

matthieusieben
Copy link
Contributor

@matthieusieben matthieusieben commented Nov 7, 2024

Part of #2999
Fixes #2952

Breaking changes

This PR changes the interfaces of the generated code in the following ways:

  • it removes the [x: string]: unknonw index signature in objects & records. Because of this, access to un-speccified fields must be explicit.
  • it makes the $type property explicit (though optional, except in open unions). This allows to properly discriminate entity types, especially when used in open unions.
  • the isMyType(value) utilities no longer type cast the value being checked. They only type the presence of the $type property. Use isValidMyType to assert the type through validation.

Note

Using main instead of msi/lex-gen-optimization (#2999) as "base branch" to allows CI to run.

Note

While reviewing, "codegen" commits can be ignored ;-)

@matthieusieben matthieusieben force-pushed the msi/lex-gen-typefixes branch 4 times, most recently from f22ff9f to 005584a Compare November 8, 2024 20:40
@matthieusieben matthieusieben marked this pull request as draft November 8, 2024 20:40
@matthieusieben matthieusieben force-pushed the msi/lex-gen-typefixes branch 6 times, most recently from f5707e0 to 3e52922 Compare November 12, 2024 15:40
@matthieusieben matthieusieben force-pushed the msi/lex-gen-typefixes branch 5 times, most recently from 4677431 to 35acae1 Compare November 14, 2024 09:23
@matthieusieben matthieusieben changed the title Make lex-gen type more precise Add explicit $type property in code gen Nov 14, 2024
@matthieusieben matthieusieben changed the base branch from main to msi/lex-gen-optimization November 14, 2024 15:30
@matthieusieben matthieusieben marked this pull request as ready for review November 14, 2024 15:45
@matthieusieben matthieusieben force-pushed the msi/lex-gen-optimization branch from ef8af3f to bebca0e Compare November 14, 2024 15:53
@matthieusieben matthieusieben force-pushed the msi/lex-gen-optimization branch from bebca0e to 0bea135 Compare November 14, 2024 16:05
@matthieusieben matthieusieben force-pushed the msi/lex-gen-typefixes branch 2 times, most recently from 66b3c0c to 6ae8660 Compare November 14, 2024 18:39
@matthieusieben matthieusieben changed the base branch from msi/lex-gen-optimization to main November 15, 2024 10:51
@matthieusieben matthieusieben force-pushed the msi/lex-gen-typefixes branch 4 times, most recently from 6bd421c to f6f0ead Compare November 15, 2024 14:11
@matthieusieben matthieusieben changed the base branch from main to msi/lex-gen-optimization November 15, 2024 16:16
Copy link
Contributor

@rafaelbsky rafaelbsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was overwhelming to review, even if the non-codegen commit. I "reviewed" it.

Comment on lines +1116 to +1120
return this.recordEmbedWrapper(
{ ...view, $type: 'app.bsky.feed.defs#generatorView' },
withTypeTag,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good example of the benefit of this change. Before, because $type was optional, the type system did not force the $type property to be present in unions.

Now, because it is no longer optional in unions, not specifying the $type here results in a type error.

@@ -2,7 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./base.json",
"compilerOptions": {
"lib": ["ES2022", "DOM", "DOM.Iterable", "ESNext.Disposable"],
"lib": ["ES2023", "DOM", "DOM.Iterable", "ESNext.Disposable"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NodeJS 18 (our minimal version) uses ES2023.

The api package already relies on ES2023 features (Array.findLast).

@matthieusieben matthieusieben force-pushed the msi/lex-gen-typefixes branch 2 times, most recently from fa6f004 to 344ac6b Compare December 16, 2024 12:54
@matthieusieben matthieusieben changed the base branch from msi/lex-gen-optimization to main December 16, 2024 12:54
@matthieusieben matthieusieben changed the base branch from main to msi/lex-gen-optimization December 18, 2024 07:56
@matthieusieben matthieusieben force-pushed the msi/lex-gen-typefixes branch 3 times, most recently from 2bbbdeb to 7ddbc91 Compare December 18, 2024 09:27
@matthieusieben matthieusieben force-pushed the msi/lex-gen-optimization branch from 6e398be to 17b3081 Compare December 18, 2024 09:39
}
} else {
return {
$type: 'com.atproto.admin.defs#repoViewNotFound',
$type: 'tools.ozone.moderation.defs#repoViewNotFound',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

com.atproto.admin.defs#repoViewNotFound does not exist.

This is a bug that was outlined by the type system thanks to the changes from this PR.

@matthieusieben matthieusieben changed the base branch from msi/lex-gen-optimization to main December 18, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generation of Omit in lex-cli types causes loss of type safety
2 participants