From ac5c2068c43ee6982552d51468b1e48b56557b40 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Sun, 22 Nov 2020 11:02:59 +0100 Subject: [PATCH] [core] Add support for TypeScript 4.1 (#23633) --- package.json | 2 +- .../src/makeStyles/makeStyles.d.ts | 19 ++++++------------- .../src/makeStyles/makeStyles.spec.tsx | 2 +- packages/material-ui-utils/src/deepmerge.ts | 8 +++++++- .../material-ui/src/styles/makeStyles.d.ts | 18 ++++++------------ 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 0ae0184a14c00e..d6dc9369deff2e 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "test:umd": "node packages/material-ui/test/umd/run.js", "test:unit": "cross-env NODE_ENV=test mocha 'packages/**/*.test.{js,ts,tsx}' 'docs/**/*.test.{js,ts,tsx}' 'scripts/**/*.test.{js,ts,tsx}' 'test/utils/**/*.test.{js,ts,tsx}'", "test:watch": "yarn test:unit --watch", - "typescript": "lerna run typescript --parallel" + "typescript": "lerna run --no-bail --parallel typescript" }, "devDependencies": { "@babel/cli": "^7.10.1", diff --git a/packages/material-ui-styles/src/makeStyles/makeStyles.d.ts b/packages/material-ui-styles/src/makeStyles/makeStyles.d.ts index a0ce04b43fcde1..b164e38cd7e3c4 100644 --- a/packages/material-ui-styles/src/makeStyles/makeStyles.d.ts +++ b/packages/material-ui-styles/src/makeStyles/makeStyles.d.ts @@ -2,22 +2,15 @@ import { ClassNameMap, Styles, WithStylesOptions } from '@material-ui/styles/wit import { Omit } from '@material-ui/types'; import { DefaultTheme } from '../defaultTheme'; -/** - * `makeStyles` where the passed `styles` do not depend on props - */ -export default function makeStyles( - style: Styles, - options?: Omit, 'withTheme'> -): (props?: any) => ClassNameMap; - -/** - * `makeStyles` where the passed `styles` do depend on props - */ export default function makeStyles< Theme = DefaultTheme, - Props extends {} = {}, + Props extends object = {}, ClassKey extends string = string >( styles: Styles, options?: Omit, 'withTheme'> -): (props: Props) => ClassNameMap; +): keyof Props extends never + ? // `makeStyles` where the passed `styles` do not depend on props + (props?: any) => ClassNameMap + : // `makeStyles` where the passed `styles` do depend on props + (props: Props) => ClassNameMap; diff --git a/packages/material-ui-styles/src/makeStyles/makeStyles.spec.tsx b/packages/material-ui-styles/src/makeStyles/makeStyles.spec.tsx index 0ec32547be1d70..c8e00982f2a567 100644 --- a/packages/material-ui-styles/src/makeStyles/makeStyles.spec.tsx +++ b/packages/material-ui-styles/src/makeStyles/makeStyles.spec.tsx @@ -81,7 +81,7 @@ import { createStyles, makeStyles } from '@material-ui/styles'; ); const UnsafeProps = (props: StyleProps) => { - // would be nice to have at least a compile time error because we forgot the argument + // @ts-expect-error const classes = useUnsafeProps(); // runtime: Can't read property color of undefined // but this would pass anyway const alsoClasses = useUnsafeProps(undefined); // runtime: Can't read property color of undefined diff --git a/packages/material-ui-utils/src/deepmerge.ts b/packages/material-ui-utils/src/deepmerge.ts index 3952225a06f2fa..8ff8d1c26ab3eb 100644 --- a/packages/material-ui-utils/src/deepmerge.ts +++ b/packages/material-ui-utils/src/deepmerge.ts @@ -1,5 +1,11 @@ export function isPlainObject(item: unknown): item is Record { - return item && typeof item === 'object' && item.constructor === Object; + return ( + item !== null && + typeof item === 'object' && + // TS thinks `item is possibly null` even though this was our first guard. + // @ts-expect-error + item.constructor === Object + ); } export interface DeepmergeOptions { diff --git a/packages/material-ui/src/styles/makeStyles.d.ts b/packages/material-ui/src/styles/makeStyles.d.ts index 07908af194078f..1f8218efa0a9da 100644 --- a/packages/material-ui/src/styles/makeStyles.d.ts +++ b/packages/material-ui/src/styles/makeStyles.d.ts @@ -2,21 +2,15 @@ import { ClassNameMap, Styles, WithStylesOptions } from '@material-ui/styles/wit import { Omit } from '@material-ui/types'; import { Theme as DefaultTheme } from './createMuiTheme'; -/** - * `makeStyles` where the passed `styles` do not depend on props - */ -export default function makeStyles( - style: Styles, - options?: Omit, 'withTheme'> -): (props?: any) => ClassNameMap; -/** - * `makeStyles` where the passed `styles` do depend on props - */ export default function makeStyles< Theme = DefaultTheme, - Props extends {} = {}, + Props extends object = {}, ClassKey extends string = string >( styles: Styles, options?: Omit, 'withTheme'> -): (props: Props) => ClassNameMap; +): keyof Props extends never + ? // `makeStyles` where the passed `styles` do not depend on props + (props?: any) => ClassNameMap + : // `makeStyles` where the passed `styles` do depend on props + (props: Props) => ClassNameMap;