-
Notifications
You must be signed in to change notification settings - Fork 72
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
Bump typescript to 4.6.4 #879
Conversation
@@ -135,7 +135,7 @@ export const GuideSection: FunctionComponent<GuideSection> = ({ | |||
const isPlaygroundUnsupported = | |||
typeof window !== 'undefined' && | |||
typeof document !== 'undefined' && | |||
!!window.MSInputMethodContext && | |||
!!(window as any).MSInputMethodContext && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love adding new code with any
. Could this be cast as object
or unknown
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, we could consider completely removing this. Please look at the follow up issue #886
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we can add the following code to index.d.ts
interface Window {
MSInputMethodContext?: any; // is IE11
}
interface Document {
documentMode?: any; // is IE11
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point about fixing it by adding to global types. The usage of any
is common to highlight areas of uncertainty or complexity in typescript. It is usually recommended to avoid it if possible
In this case it should be safe to mark some pain point by as any
. It could be more scary to cleanup index.d.ts
in future, than cleanup local usage of any
. My preference is to keep theindex.d.ts
nice and clean.
On other hand I cant see better cast here and we have issue #886 for refactoring
return 'innerText' in element | ||
? element.innerText | ||
: // (this line left here to satisfy Prettier since a ts-ignore is used on the next line) | ||
// @ts-ignore TypeScript thinks element.innerText always exists, however it doesn't in jest/jsdom environment | ||
element.textContent || undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love that we're able to get rid of this @ts-ignore
@@ -115,7 +115,7 @@ export const OuiPinnableListGroup: FunctionComponent<OuiPinnableListGroupProps> | |||
); | |||
|
|||
// Add the pinning action unless the item has it's own extra action | |||
if (onPinClick && !itemProps.extraAction && pinnable) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is onPinClick
removed here? From a brief look at the code, it seems like it should be null checked
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The onPinClick
is defined as required prop for OuiPinnableListGroup
component. Typescript will give error, if onPinClick
is not provided. So no extra validation needed. I hope we can benefit from type checking here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compile-time warning cannot take the place of proper guarding. There are many scenarios that TS will not be able check during compiling.
That said, onPinClick
check shouldn't be up there but should be added down.
if (onPinClick) itemProps.extraAction.onClick = () => onPinClick(item);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I committed the above suggestion.
tsconfig.json
Outdated
@@ -40,6 +40,9 @@ | |||
// Disallow inconsistently-cased references to the same file. | |||
"forceConsistentCasingInFileNames": true, | |||
|
|||
// Keep type of the variable in a catch clause as `any` | |||
"useUnknownInCatchVariables": false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want to keep it as any? Is there a significant downside of using unknown
? Will it involve a substantial amount of extra work? Could it be done in a followup task?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typescript can enhance code quality, but it is also important to keep balance between type safety and ease of use
The useUnknownInCatchVariables
option is useful because it enforces a higher level of type safety in catch
blocks. However, enabling this option can also lead to additional complexity in code. We would need to add extra type checking code in each catch
block.
Let me show on examples
It is common to expect catch variable as instance of Error:
try {
throw Error('error message');
} catch (error) {
console.log(error.message); // => LOG: 'error message'
}
but it is not always the case:
try {
throw 'error message';
} catch (error) {
console.log(error.message); // => LOG: undefined
}
useUnknownInCatchVariables
is intended to catch errors like this:
try {
throw 123;
} catch (error) {
if (err instanceof Error) {
console.log(err.message);
} else if (typeof err === 'string') {
console.log(err);
} else {
console.log('Unexpected error')
}
}
or:
try {
throw 123;
} catch (error) {
const e = error instanceof Error ? error : Error( `Unexpected Error: ${error}`);
console.log(e.message);
}
Same time it is much easier to avoid checks by ignoring types:
try {
throw new OwnError(123);
} catch (error: unknown) {
console.log((error as OwnError).message);
}
or even:
try {
throw new Error(123);
} catch (error : any) {
console.log(error.message);
}
While enabling useUnknownInCatchVariables
could potentially catch errors, it might also introduce extra complexity into codebase. I am cautious about making this change, especially since the useUnknownInCatchVariables
is enabled by default.
I would be happy to make this change, if benefits of enabling useUnknownInCatchVariables
outweigh potential downsides. Please let me know if you want to proceed with this and I will create a followup task
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good idea to reduce complexity and remove conditions inside the catch block, but I think type safety and robust error handling have a higher weight than reducing complexity like that, so for me, it's better to use unknown
rather than any
for error variables in catch clauses
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be some confusion regarding this change. I have created issue #887 to do refactoring with useUnknownInCatchVariables
in separate task. It will help to keep this pull request limited.
Could you please review it @BSFishy @SergeyMyssak ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we can avoid this change? I see that you have created an issue to remove it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useUnknownInCatchVariables
is not in tsconfig anymore. We have changes in this pull request to close #887
@@ -159,8 +159,8 @@ | |||
"@types/react-router-dom": "^5.3.3", | |||
"@types/url-parse": "^1.4.8", | |||
"@types/uuid": "^9.0.1", | |||
"@typescript-eslint/eslint-plugin": "^4.8.1", | |||
"@typescript-eslint/parser": "^4.8.1", | |||
"@typescript-eslint/eslint-plugin": "^5.62.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @typescript-eslint/eslint-plugin: "^6.0.0"
requires at least node v16, but .nvmrc
contains 14.20.0
@@ -135,7 +135,7 @@ export const GuideSection: FunctionComponent<GuideSection> = ({ | |||
const isPlaygroundUnsupported = | |||
typeof window !== 'undefined' && | |||
typeof document !== 'undefined' && | |||
!!window.MSInputMethodContext && | |||
!!(window as any).MSInputMethodContext && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we can add the following code to index.d.ts
interface Window {
MSInputMethodContext?: any; // is IE11
}
interface Document {
documentMode?: any; // is IE11
}
{...(rest as PropsForButton)} | ||
onClick={onClick}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure we should put onClick
after {{...(rest as PropsForButton)}
? I understand that TS gives an error there, but it would still be good to understand why it started giving it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typescript highlights that onClick
is specified more than once, so usage will be overwritten
This is due usage of as PropsForButton
, which reintroduces the onClick
while it is already picked
ButtonHTMLAttributes<HTMLButtonElement>,
'onClick' | 'type'
> & {
onClick: MouseEventHandler<HTMLButtonElement> | undefined;
};
Provided solution is easer than {...(rest as Omit<PropsForButton, 'onClick'}
or refactoring of the PropsForButton
or keeping onClick
in rest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand why this interface is written as it is, I think it could be simplified and the TS error would disappear:
type PropsForButton = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'type'>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typing is changed to suggested
tsconfig.json
Outdated
@@ -40,6 +40,9 @@ | |||
// Disallow inconsistently-cased references to the same file. | |||
"forceConsistentCasingInFileNames": true, | |||
|
|||
// Keep type of the variable in a catch clause as `any` | |||
"useUnknownInCatchVariables": false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good idea to reduce complexity and remove conditions inside the catch block, but I think type safety and robust error handling have a higher weight than reducing complexity like that, so for me, it's better to use unknown
rather than any
for error variables in catch clauses
{...(rest as PropsForButton)} | ||
onClick={onClick}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand why this interface is written as it is, I think it could be simplified and the TS error would disappear:
type PropsForButton = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'type'>;
tsconfig.json
Outdated
@@ -40,6 +40,9 @@ | |||
// Disallow inconsistently-cased references to the same file. | |||
"forceConsistentCasingInFileNames": true, | |||
|
|||
// Keep type of the variable in a catch clause as `any` | |||
"useUnknownInCatchVariables": false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we can avoid this change? I see that you have created an issue to remove it
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
bump typescript-eslint/parser: 5.62.0 node 16 is required for typescript-eslint: 6.0.0 Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
Signed-off-by: Danila Gulderov <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I don't see any public API changes, so this should be good to go. I'd also like to get another pair of eyes or two on this, as its blast radius might be a little bit larger than we usually have. Pinging people now |
Signed-off-by: Matt Provost <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
: // (this line left here to satisfy Prettier since a ts-ignore is used on the next line) | ||
// @ts-ignore TypeScript thinks element.innerText always exists, however it doesn't in jest/jsdom environment | ||
element.textContent || undefined; | ||
return 'innerText' in element ? element.innerText : element.textContent || ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we're changing the return type to string
from string | undefined
- is this intentional, and does it have any effect in caller contexts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While i think it will impact the caller contexts, i believe the previous behavior of returning undefined was wrong.
const message = e instanceof Error ? e.message : String(e); | ||
|
||
error( | ||
`Invalid value \`${expression}\` set for field \`${field}\` - ${e.message}`, | ||
`Invalid value \`${expression}\` set for field \`${field}\` - ${message}`, | ||
location | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's strengthen this a bit:
const message = (e instanceof Error ? e.message || e.code : String(e)).trim();
error(
`Invalid value \`${expression}\` set for field \`${field}\`${message ? ` - ${message}` : '' }`,
location
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I committed a nicer version of the above suggestion.
PS, tsc
didn't like that. Will adjust it.
Move CHANGELOG entry to the unreleased section Signed-off-by: Miki <[email protected]>
Update pinnable_list_group.tsx Signed-off-by: Miki <[email protected]>
Update default_syntax.ts Signed-off-by: Miki <[email protected]>
Update default_syntax.ts Signed-off-by: Miki <[email protected]>
Signed-off-by: Miki <[email protected]>
* typescript bump: 4.1.6 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.2.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.3.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.4.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.5.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.6.4 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md updated Signed-off-by: Danila Gulderov <[email protected]> * bump typescript-eslint/eslint-plugin: 5.62.0 bump typescript-eslint/parser: 5.62.0 node 16 is required for typescript-eslint: 6.0.0 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md update Signed-off-by: Danila Gulderov <[email protected]> * move typescript update to regular section Signed-off-by: Danila Gulderov <[email protected]> * changelog message improvement Signed-off-by: Danila Gulderov <[email protected]> * fix typings Signed-off-by: Danila Gulderov <[email protected]> * cleanup useUnknownInCatchVariables Signed-off-by: Danila Gulderov <[email protected]> * logical or => nullish coalescing Signed-off-by: Danila Gulderov <[email protected]> * wrap with String Signed-off-by: Danila Gulderov <[email protected]> * Update CHANGELOG.md Move CHANGELOG entry to the unreleased section Signed-off-by: Miki <[email protected]> * Add back `onPinClick` check in `OuiPinnableListGroup` Update pinnable_list_group.tsx Signed-off-by: Miki <[email protected]> * Enhance error message extraction in `validateFieldValue` Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Remove e.code from default_syntax.ts Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Lint default_syntax.ts Signed-off-by: Miki <[email protected]> --------- Signed-off-by: Danila Gulderov <[email protected]> Signed-off-by: Matt Provost <[email protected]> Signed-off-by: Miki <[email protected]> Co-authored-by: Matt Provost <[email protected]> Co-authored-by: Josh Romero <[email protected]> Co-authored-by: Miki <[email protected]> (cherry picked from commit 1fe770c) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md
* typescript bump: 4.1.6 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.2.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.3.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.4.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.5.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.6.4 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md updated Signed-off-by: Danila Gulderov <[email protected]> * bump typescript-eslint/eslint-plugin: 5.62.0 bump typescript-eslint/parser: 5.62.0 node 16 is required for typescript-eslint: 6.0.0 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md update Signed-off-by: Danila Gulderov <[email protected]> * move typescript update to regular section Signed-off-by: Danila Gulderov <[email protected]> * changelog message improvement Signed-off-by: Danila Gulderov <[email protected]> * fix typings Signed-off-by: Danila Gulderov <[email protected]> * cleanup useUnknownInCatchVariables Signed-off-by: Danila Gulderov <[email protected]> * logical or => nullish coalescing Signed-off-by: Danila Gulderov <[email protected]> * wrap with String Signed-off-by: Danila Gulderov <[email protected]> * Update CHANGELOG.md Move CHANGELOG entry to the unreleased section Signed-off-by: Miki <[email protected]> * Add back `onPinClick` check in `OuiPinnableListGroup` Update pinnable_list_group.tsx Signed-off-by: Miki <[email protected]> * Enhance error message extraction in `validateFieldValue` Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Remove e.code from default_syntax.ts Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Lint default_syntax.ts Signed-off-by: Miki <[email protected]> --------- Signed-off-by: Danila Gulderov <[email protected]> Signed-off-by: Matt Provost <[email protected]> Signed-off-by: Miki <[email protected]> Co-authored-by: Matt Provost <[email protected]> Co-authored-by: Josh Romero <[email protected]> Co-authored-by: Miki <[email protected]> (cherry picked from commit 1fe770c) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Thanks @gulderov ! This was a very valuable contribution! |
* typescript bump: 4.1.6 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.2.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.3.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.4.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.5.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.6.4 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md updated Signed-off-by: Danila Gulderov <[email protected]> * bump typescript-eslint/eslint-plugin: 5.62.0 bump typescript-eslint/parser: 5.62.0 node 16 is required for typescript-eslint: 6.0.0 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md update Signed-off-by: Danila Gulderov <[email protected]> * move typescript update to regular section Signed-off-by: Danila Gulderov <[email protected]> * changelog message improvement Signed-off-by: Danila Gulderov <[email protected]> * fix typings Signed-off-by: Danila Gulderov <[email protected]> * cleanup useUnknownInCatchVariables Signed-off-by: Danila Gulderov <[email protected]> * logical or => nullish coalescing Signed-off-by: Danila Gulderov <[email protected]> * wrap with String Signed-off-by: Danila Gulderov <[email protected]> * Update CHANGELOG.md Move CHANGELOG entry to the unreleased section Signed-off-by: Miki <[email protected]> * Add back `onPinClick` check in `OuiPinnableListGroup` Update pinnable_list_group.tsx Signed-off-by: Miki <[email protected]> * Enhance error message extraction in `validateFieldValue` Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Remove e.code from default_syntax.ts Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Lint default_syntax.ts Signed-off-by: Miki <[email protected]> --------- Signed-off-by: Danila Gulderov <[email protected]> Signed-off-by: Matt Provost <[email protected]> Signed-off-by: Miki <[email protected]> Co-authored-by: Matt Provost <[email protected]> Co-authored-by: Josh Romero <[email protected]> Co-authored-by: Miki <[email protected]> Signed-off-by: ShatilKhan <[email protected]>
* typescript bump: 4.1.6 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.2.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.3.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.4.4 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.5.5 Signed-off-by: Danila Gulderov <[email protected]> * typescript bump: 4.6.4 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md updated Signed-off-by: Danila Gulderov <[email protected]> * bump typescript-eslint/eslint-plugin: 5.62.0 bump typescript-eslint/parser: 5.62.0 node 16 is required for typescript-eslint: 6.0.0 Signed-off-by: Danila Gulderov <[email protected]> * CHANGELOG.md update Signed-off-by: Danila Gulderov <[email protected]> * move typescript update to regular section Signed-off-by: Danila Gulderov <[email protected]> * changelog message improvement Signed-off-by: Danila Gulderov <[email protected]> * fix typings Signed-off-by: Danila Gulderov <[email protected]> * cleanup useUnknownInCatchVariables Signed-off-by: Danila Gulderov <[email protected]> * logical or => nullish coalescing Signed-off-by: Danila Gulderov <[email protected]> * wrap with String Signed-off-by: Danila Gulderov <[email protected]> * Update CHANGELOG.md Move CHANGELOG entry to the unreleased section Signed-off-by: Miki <[email protected]> * Add back `onPinClick` check in `OuiPinnableListGroup` Update pinnable_list_group.tsx Signed-off-by: Miki <[email protected]> * Enhance error message extraction in `validateFieldValue` Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Remove e.code from default_syntax.ts Update default_syntax.ts Signed-off-by: Miki <[email protected]> * Lint default_syntax.ts Signed-off-by: Miki <[email protected]> --------- Signed-off-by: Danila Gulderov <[email protected]> Signed-off-by: Matt Provost <[email protected]> Signed-off-by: Miki <[email protected]> Co-authored-by: Matt Provost <[email protected]> Co-authored-by: Josh Romero <[email protected]> Co-authored-by: Miki <[email protected]> Signed-off-by: ShatilKhan <[email protected]>
Description
Bump typescript to 4.6.4
Issues Resolved
closes #888
closes #887
Check List
yarn lint
yarn test-unit
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.