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

[typescript-to-proptypes] Support using Omit on types with conditional properties #41033

Merged

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Feb 9, 2024

I don't know why TS does not work correctly with this type combination and this fix is super ugly.
But it works 馃槵

@mui-bot
Copy link

mui-bot commented Feb 9, 2024

Netlify deploy preview

https://deploy-preview-41033--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 馃毇 dangerJS against 5b44a73

@flaviendelangle flaviendelangle changed the title [typescript-to-proptypes] Support ts.TypeFlags.IncludesWildcard [typescript-to-proptypes] Support using Omit on types with conditional properties Feb 9, 2024
@flaviendelangle flaviendelangle self-assigned this Feb 9, 2024
@flaviendelangle flaviendelangle added the core Infrastructure work going on behind the scenes label Feb 9, 2024
@@ -55,6 +55,48 @@ function getSymbolDocumentation({
return comment !== '' ? comment : undefined;
}

function getType({
Copy link
Member Author

Choose a reason for hiding this comment

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

I just moved this code without any change

@@ -206,6 +248,38 @@ function checkType({
return createLiteralType({ jsDoc, value: 'null' });
}

if (type.flags & ts.TypeFlags.IndexedAccess) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The problem is that, for conditional types inside an Omit, TS does not resolve its value, it just says "it's looks like A['key']".

My ugly solution is to retrieve its resolved parent with objectType, and if this parent looks like type A<T> = T extends B ? C : D, then I resolve both sides of the ternary and for each I get the property value and create an union.

Copy link
Member

Choose a reason for hiding this comment

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

IMHO, it seems only partially hacky.
We have a known case that we handle conditionally, it does not produce any side effects in the Core repo and none in the X repo鈥攕ounds like a solution to me. 馃 馃檲 馃挕

Copy link
Member

Choose a reason for hiding this comment

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

Make sens for the generation of proptypes 馃憤

I assume TS only keep the index pointing to it in order to easily manage the case where the condition get defined. Which we don't care for proptypes

@flaviendelangle flaviendelangle marked this pull request as ready for review February 12, 2024 08:14
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

Nice and very needed improvement! 馃憤 馃殌

@@ -206,6 +248,38 @@ function checkType({
return createLiteralType({ jsDoc, value: 'null' });
}

if (type.flags & ts.TypeFlags.IndexedAccess) {
Copy link
Member

Choose a reason for hiding this comment

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

IMHO, it seems only partially hacky.
We have a known case that we handle conditionally, it does not produce any side effects in the Core repo and none in the X repo鈥攕ounds like a solution to me. 馃 馃檲 馃挕

@@ -206,6 +248,38 @@ function checkType({
return createLiteralType({ jsDoc, value: 'null' });
}

if (type.flags & ts.TypeFlags.IndexedAccess) {
Copy link
Member

Choose a reason for hiding this comment

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

Make sens for the generation of proptypes 馃憤

I assume TS only keep the index pointing to it in order to easily manage the case where the condition get defined. Which we don't care for proptypes

@flaviendelangle flaviendelangle merged commit c5b4eba into mui:master Feb 14, 2024
21 checks passed
@flaviendelangle flaviendelangle deleted the proptypes-pickerstextfield branch February 14, 2024 13:00
@flaviendelangle flaviendelangle mentioned this pull request Feb 14, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants