From 6e192ad4267fe866e826ba7551cdd75276e45b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Magalh=C3=A3es?= <357835+vhfmag@users.noreply.github.com> Date: Sun, 26 Nov 2017 03:47:56 -0300 Subject: [PATCH] Enable TypeScript's strict mode to fix bugs and preemptively catch new ones (#50) * Fix implicit any type error * build(tsconfig): Enable strict mode to enforce good practices * fix: Handle strict mode typescript errors --- src/DeviceMotion/DeviceMotion.tsx | 12 ++++++------ src/Mailto.tsx | 4 ++-- src/Scroll/Scroll.tsx | 4 ++-- src/WindowSize/WindowSize.tsx | 6 +++--- src/utils/debounce.ts | 2 +- src/utils/throttle.ts | 2 +- tsconfig.json | 1 + 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/DeviceMotion/DeviceMotion.tsx b/src/DeviceMotion/DeviceMotion.tsx index 9304eee..eeb860a 100644 --- a/src/DeviceMotion/DeviceMotion.tsx +++ b/src/DeviceMotion/DeviceMotion.tsx @@ -10,10 +10,10 @@ import { SharedRenderProps } from '../types'; import { isEmptyChildren } from '../utils'; export interface DeviceMotionProps { - acceleration: DeviceAcceleration; - accelerationIncludingGravity: DeviceAcceleration; - rotationRate: DeviceRotationRate; - interval: number; + acceleration: DeviceAcceleration | null; + accelerationIncludingGravity: DeviceAcceleration | null; + rotationRate: DeviceRotationRate | null; + interval: number | null; } export class DeviceMotion extends React.Component< @@ -49,11 +49,11 @@ export class DeviceMotion extends React.Component< }; componentDidMount() { - window.addEventListener('deviceMotion', this.handleDeviceMotion, true); + window.addEventListener('devicemotion', this.handleDeviceMotion, true); } componentWillUnmount() { - window.removeEventListener('deviceMotion', this.handleDeviceMotion); + window.removeEventListener('devicemotion', this.handleDeviceMotion); } render() { diff --git a/src/Mailto.tsx b/src/Mailto.tsx index 31a9872..f6041bf 100644 --- a/src/Mailto.tsx +++ b/src/Mailto.tsx @@ -34,8 +34,8 @@ export const Mailto: React.SFC = ({ , ScrollProps > { - static defaultProps = { + static defaultProps: Partial = { throttle: 100, }; @@ -32,7 +32,7 @@ export class Scroll extends React.Component< handleWindowScroll = throttle(() => { this.setState({ x: window.scrollX, y: window.scrollY }); - }, this.props.throttle); + }, this.props.throttle!); componentDidMount() { this.handleWindowScroll(); diff --git a/src/WindowSize/WindowSize.tsx b/src/WindowSize/WindowSize.tsx index ab5d4a6..fa1c276 100644 --- a/src/WindowSize/WindowSize.tsx +++ b/src/WindowSize/WindowSize.tsx @@ -23,15 +23,15 @@ export class WindowSize extends React.Component< WindowSizeConfig & SharedRenderProps, WindowSizeProps > { - static defaultProps = { - debounce: 100, + static defaultProps: Partial = { + throttle: 100, }; state: WindowSizeProps = { width: 0, height: 0 }; handleWindowSize = throttle(() => { this.setState({ width: window.innerWidth, height: window.innerHeight }); - }, this.props.throttle); + }, this.props.throttle!); componentDidMount() { this.handleWindowSize(); diff --git a/src/utils/debounce.ts b/src/utils/debounce.ts index 76612f7..f6c72aa 100644 --- a/src/utils/debounce.ts +++ b/src/utils/debounce.ts @@ -17,7 +17,7 @@ */ export const debounce = (func: Function, wait: number, immediate?: boolean) => { let timeout: any; - return function() { + return function(this: any) { var context = this, args = arguments; var later = function() { diff --git a/src/utils/throttle.ts b/src/utils/throttle.ts index 9ad6c08..b336282 100644 --- a/src/utils/throttle.ts +++ b/src/utils/throttle.ts @@ -6,7 +6,7 @@ * @param wait time */ -export function throttle(func: Function, wait: Number) { +export function throttle(this: any, func: Function, wait: Number) { let timeout: number | null = null; let callbackArgs: IArguments | null = null; const context = this; diff --git a/tsconfig.json b/tsconfig.json index d30378c..0a9bccc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,6 +19,7 @@ "removeComments": true, "sourceMap": true, "pretty": true, + "strict": true, "stripInternal": true, "target": "es5",