Skip to content

More type safety for language keys#93

Open
JohannesMeierSE wants to merge 17 commits intomainfrom
jm/more-type-safety
Open

More type safety for language keys#93
JohannesMeierSE wants to merge 17 commits intomainfrom
jm/more-type-safety

Conversation

@JohannesMeierSE
Copy link
Collaborator

@JohannesMeierSE JohannesMeierSE commented Sep 4, 2025

This PR increases the TypeScript type-safety and can be seen as a follow-up of #90

  • The TypirSpecifics now contain the property LanguageKeys: Record<string, unknown>; to make language keys explicit. This is the precondition for the following improvements:
  • make properties which are using language keys type-safe
  • exploit Specifics['LanguageKeys'] to simplify inference rules for functions and operators: Now it is possible to skip the TypeScript type when using only languageKey and no filter. @Lotes You sketched this idea some time ago and now we have everything to make your idea happen 🙂
  • Since we have Specifics['LanguageKeys'] now even in Typir (core), we support the features addValidationRulesForAstNodes and addInferenceRulesForAstNodes (which are Typir-Langium only so far) also in Typir (core), but slightly renamed ("Language" instead of "Ast").

Contributions of this PR which are not related to language keys:

  • languageProperty supports only valid property names of the given languageNode inside validations. These properties could be reduced by the new TypirSpecifics['OmittedLanguageNodeProperties'].
  • wrote some more documentation
  • fixed watch script

Open issue:

  • When using AstNode in Typir-Langium as key for registering validation/inference rules, the TypeScript compiler infers any instead of AstNode for the current language node.

I recommend to review this PR commit by commit.

@JohannesMeierSE JohannesMeierSE added this to the v0.4 milestone Sep 4, 2025
Copy link
Collaborator

@Lotes Lotes left a comment

Choose a reason for hiding this comment

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

I like the type specific bundle. It gives the opportunity to carry along language-specific types, similar to the dependency injection, which bundles values of services.
I also like those lookup functions, so that you do not have to break apart the specifics on your own.

Most stuff is a replacement for the old types, but I found one little thing, where I wonder if we can make this better.

Copy link
Member

@insafuhrmann insafuhrmann left a comment

Choose a reason for hiding this comment

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

Thanks @JohannesMeierSE, I have only some detail remarks as you can see in the comments. Please feel free to ping me for an approving re-review when conflicts are resolved and the PR is updated.

@JohannesMeierSE
Copy link
Collaborator Author

@insafuhrmann @Lotes Thanks for your reviews! I just pushed updates, added the jointly discussed calculation of language key unions and rebased the branch on main.

@JohannesMeierSE
Copy link
Collaborator Author

What do you think about supporting addValidationRulesForAstNodes also in Typir (core)? see the open issues in the PR description above

Copy link
Member

@insafuhrmann insafuhrmann left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @JohannesMeierSE! I think they are very nice and I also tried to answer your question regarding the possible extension in a comment in the code.

Regarding supporting the features addValidationRulesForAstNodes and addInferenceRulesForAstNodes also in core: I am in favour of this. Let's do it.

Copy link
Member

@insafuhrmann insafuhrmann left a comment

Choose a reason for hiding this comment

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

Thanks for the additions @JohannesMeierSE. Let's discuss the intersection problem

* Contains properties of language nodes, which shall be omitted for validation issues,
* i.e. these properties are not possible to attach validation markers to.
*
* The types given here are usable as (object) keys in general and therefore enable concrete, inheriting `TypirSpecifics` to specify more concrete keys.
Copy link
Member

Choose a reason for hiding this comment

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

concrete ...? Is there a word missing?

concrete specifications?

[K in LanguageKey<Specifics>]?: Specifics['LanguageKeys'][K] extends Specifics['LanguageType'] ? ValidationRule<Specifics, Specifics['LanguageKeys'][K]> | Array<ValidationRule<Specifics, Specifics['LanguageKeys'][K]>> : never
} & {
export type LangiumValidationRules<Specifics extends TypirLangiumSpecifics> = ValidationRulesForLanguageKeys<Specifics> & {
// TODO nodes inside ValidationRules are typed by the TypeScript compiler as `any` not as `AstNode`
Copy link
Member

Choose a reason for hiding this comment

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

Should we maybe put this on the agenda for the upcoming meeting to discuss possible solutions?

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.

3 participants