From e0642a9d0d69ea1276e30ca442cfbd2d3f1a60fd Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 10 Oct 2019 22:20:05 +0200 Subject: [PATCH] fix: improved typing (#174) --- package.json | 1 + src/App/App.test.tsx | 14 +++++++------- src/App/App.tsx | 8 ++++---- src/App/AppError.tsx | 8 ++++---- src/components/AvatarTooltip/AvatarTooltip.tsx | 2 +- .../CopyToClipBoard/CopyToClipBoard.test.tsx | 4 ++-- src/components/CopyToClipBoard/CopyToClipBoard.tsx | 2 +- src/components/Engines/Engines.tsx | 2 +- src/components/Footer/Footer.test.tsx | 4 ++-- src/components/Login/Login.tsx | 4 ++-- src/index.tsx | 2 +- src/pages/Version/Version.tsx | 2 +- src/utils/calls.ts | 2 +- src/utils/package.ts | 4 ++-- src/utils/url.ts | 2 +- tsconfig.strict.json | 6 ++++++ 16 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 tsconfig.strict.json diff --git a/package.json b/package.json index 5bd151f..0bb6ec8 100644 --- a/package.json +++ b/package.json @@ -151,6 +151,7 @@ "scripts": { "type-check": "tsc --noEmit --pretty", "type-check:watch": "npm run type-check -- --watch", + "type-check-strict:watch": "tsc --project ./tsconfig.strict.json --noEmit --pretty --watch", "release": "standard-version -a", "test:clean": "npx jest --clearCache", "test:acceptance": "codeceptjs run --steps", diff --git a/src/App/App.test.tsx b/src/App/App.test.tsx index 3050589..f197969 100644 --- a/src/App/App.test.tsx +++ b/src/App/App.test.tsx @@ -1,27 +1,27 @@ import React from 'react'; -import { mount } from 'enzyme'; +import { mount, ReactWrapper } from 'enzyme'; import storage from '../utils/storage'; import { generateTokenWithTimeRange } from '../../jest/unit/components/__mocks__/token'; -import App from './App'; +import App, { AppStateInterface } from './App'; jest.mock('../utils/storage', () => { class LocalStorageMock { - private store: object; + private store: Record; public constructor() { this.store = {}; } public clear(): void { this.store = {}; } - public getItem(key): unknown { + public getItem(key: string): unknown { return this.store[key] || null; } - public setItem(key, value): void { + public setItem(key: string, value: string): void { this.store[key] = value.toString(); } - public removeItem(key): void { + public removeItem(key: string): void { delete this.store[key]; } } @@ -33,7 +33,7 @@ jest.mock('../utils/api', () => ({ })); describe('App', () => { - let wrapper; + let wrapper: ReactWrapper<{}, AppStateInterface, App>; beforeEach(() => { wrapper = mount(); diff --git a/src/App/App.tsx b/src/App/App.tsx index 10d2ebb..d9f82fc 100644 --- a/src/App/App.tsx +++ b/src/App/App.tsx @@ -48,7 +48,7 @@ export default class App extends Component<{}, AppStateInterface> { } // eslint-disable-next-line no-unused-vars - public componentDidUpdate(_, prevState): void { + public componentDidUpdate(_: AppStateInterface, prevState: AppStateInterface): void { const { isUserLoggedIn } = this.state; if (prevState.isUserLoggedIn !== isUserLoggedIn) { this.loadOnHandler(); @@ -99,7 +99,7 @@ export default class App extends Component<{}, AppStateInterface> { } }; - public setLoading = isLoading => + public setLoading = (isLoading: boolean) => this.setState({ isLoading, }); @@ -118,7 +118,7 @@ export default class App extends Component<{}, AppStateInterface> { * handles login * Required by:
*/ - public handleDoLogin = async (usernameValue, passwordValue) => { + public handleDoLogin = async (usernameValue: string, passwordValue: string) => { const { username, token, error } = await makeLogin(usernameValue, passwordValue); if (username && token) { @@ -135,7 +135,7 @@ export default class App extends Component<{}, AppStateInterface> { } }; - public setLoggedUser = username => { + public setLoggedUser = (username: string) => { this.setState({ user: { username, diff --git a/src/App/AppError.tsx b/src/App/AppError.tsx index 5042e40..7d2175c 100644 --- a/src/App/AppError.tsx +++ b/src/App/AppError.tsx @@ -6,17 +6,17 @@ export interface ErrorProps { export interface ErrorAppState { hasError: boolean; - error: any; - info: any; + error: Error | null; + info: object | null; } export default class ErrorBoundary extends Component { - constructor(props) { + constructor(props: ErrorProps) { super(props); this.state = { hasError: false, error: null, info: null }; } - public componentDidCatch(error, info) { + public componentDidCatch(error: Error, info: object) { this.setState({ hasError: true, error, info }); } diff --git a/src/components/AvatarTooltip/AvatarTooltip.tsx b/src/components/AvatarTooltip/AvatarTooltip.tsx index 2f2f5fa..2e04058 100644 --- a/src/components/AvatarTooltip/AvatarTooltip.tsx +++ b/src/components/AvatarTooltip/AvatarTooltip.tsx @@ -14,7 +14,7 @@ export interface AvatarDeveloper { const AvatarTooltip: FC = ({ name, packageName, version, avatar, email }) => { const avatarComponent = ; - function renderLinkForMail(email, avatarComponent, packageName, version): JSX.Element { + function renderLinkForMail(email: string, avatarComponent: JSX.Element, packageName: string, version: string): JSX.Element { if (!email || isEmail(email) === false) { return avatarComponent; } diff --git a/src/components/CopyToClipBoard/CopyToClipBoard.test.tsx b/src/components/CopyToClipBoard/CopyToClipBoard.test.tsx index a6a3bb4..fe530dd 100644 --- a/src/components/CopyToClipBoard/CopyToClipBoard.test.tsx +++ b/src/components/CopyToClipBoard/CopyToClipBoard.test.tsx @@ -1,11 +1,11 @@ import React from 'react'; -import { mount } from 'enzyme'; +import { mount, ReactWrapper } from 'enzyme'; import CopyToClipBoard from './CopyToClipBoard'; import { CopyIcon } from './styles'; describe(' component', () => { - let wrapper; + let wrapper: ReactWrapper; beforeEach(() => { wrapper = mount(); diff --git a/src/components/CopyToClipBoard/CopyToClipBoard.tsx b/src/components/CopyToClipBoard/CopyToClipBoard.tsx index 3e426d8..cf8b95a 100644 --- a/src/components/CopyToClipBoard/CopyToClipBoard.tsx +++ b/src/components/CopyToClipBoard/CopyToClipBoard.tsx @@ -12,7 +12,7 @@ interface Props { children?: React.ReactNode; } -const renderText = (text, children): JSX.Element => { +const renderText = (text: string, children: React.ReactNode): JSX.Element => { if (children) { return {children}; } diff --git a/src/components/Engines/Engines.tsx b/src/components/Engines/Engines.tsx index af5f995..885bf1e 100644 --- a/src/components/Engines/Engines.tsx +++ b/src/components/Engines/Engines.tsx @@ -58,7 +58,7 @@ class Engine extends Component { return {items}; }; - private renderListItems = (heading, text) => { + private renderListItems = (heading: string, text: string) => { return ( {text.split('-').join(' ')}}> diff --git a/src/components/Footer/Footer.test.tsx b/src/components/Footer/Footer.test.tsx index d8f9240..bbf01c6 100644 --- a/src/components/Footer/Footer.test.tsx +++ b/src/components/Footer/Footer.test.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { mount } from 'enzyme'; +import { mount, ReactWrapper } from 'enzyme'; import Footer from './Footer'; @@ -8,7 +8,7 @@ jest.mock('../../../package.json', () => ({ })); describe('