Skip to content

Fix satisfies example in docs - #2955

Closed
nickbarrett645 wants to merge 2 commits into
microsoft:v2from
nickbarrett645:fix-satisfies-exmaple-in-docs
Closed

Fix satisfies example in docs#2955
nickbarrett645 wants to merge 2 commits into
microsoft:v2from
nickbarrett645:fix-satisfies-exmaple-in-docs

Conversation

@nickbarrett645

Copy link
Copy Markdown

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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Member

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

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