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

Fix satisfies example in docs #2955

Conversation

nickbarrett645
Copy link

The code example for the satisfies keyword indicates that there would be an error related to
const redComponent = palette.red.at(0); when the type is added to the palette object because red could be type RGB (Array) or a string. However, there wouldn't be an error there because the function at is on both Array and String prototypes. There would however be an error on the next line in the example because the toUpperCase function does only apply to Strings.

Nick Barrett added 2 commits October 17, 2023 23:53
…t produce the error indicated because the at function works on strings
@orta
Copy link
Contributor

orta commented Oct 18, 2023

This example could use twoslash being turned on to show the errors (and hovers) but the original looks correct to me still as its talking about the lack of type refinement due to as const not being applied (because it is string | RBG in the type system)

@nickbarrett645
Copy link
Author

Here is what I am seeing when I implement the example from the docs:
Screenshot 2023-10-18 at 9 35 16 AM

The error is not on const redComponent = palette.red.at(0) because the function at is applicable to both strings and arrays. The type of palette defines the value in the record to be either a string or an RGB type, which is an array, so either way the at function will be available on whatever value exists. We do see an error on const greenNormalized = palette.green.toUpperCase() because toUpperCase only applies to strings and not arrays. There for that function may not be available depending on the type of the value.

The docs originally say

// But we now have an undesirable error here - 'palette.red' "could" be a string.
const redComponent = palette.red.at(0);

This is not correct because in this example is does not matter if the value of red in the palette object is a string or an array because at exists on either type.

The change is just to clarify which line the error is seen on.

@jakebailey
Copy link
Member

Thanks for sending this, but I merged #3050 instead as I felt that was clearer.

@jakebailey jakebailey closed this Jun 5, 2024
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