question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript error with v4.0.0-alpha.8

See original GitHub issue

The following code worked perfectly fine in alpha.7, but is broken in alpha.8. Can’t tell if it’s React signatures that have changed or MUI’s:

export interface ChartHeaderProps {
    onChange: (event: React.ChangeEvent<HTMLSelectElement>) => void;
}

export const ChartHeader = ({onTimePeriodChange}: ChartHeaderProps) => {
    return (
        <div className={classes.rhs}>
            <FormControl className={classes.formControl}>
                <Select
                    name="period"
                    value={timePeriod}
                    onChange={onChange}
                >
                    ...
                </Select>
            </FormControl>
        </div>
    )
};

Now I am getting the following TypeScript error:

Type ‘(event: ChangeEvent<HTMLSelectElement>) => void’ is not assignable to type ‘(event: ChangeEvent<{ name?: string | undefined; value: unknown; }>, child: ReactNode) => void’

I got past the error by changing the interface as follows, but that is a fairly complex signature for something so simple.

export interface ChartHeaderProps {
    onTimePeriodChange: (event: ChangeEvent<{ name?: string | undefined; value: unknown; }>, child: ReactNode) => void;
}

And it doesn’t solve the problem. In the actual event handler, I am no longer able to use event.target.value, I get yet another TypeScript error:

Type ‘unknown’ is not assignable to type ‘(prevState: string) => string’

P.S.

There was one more TypeScript related error with makeStyles:

TS2571: Object is of type 'unknown`

I got around that one by adding the following import:

import { Theme } from '@material-ui/core/styles';

and then supplying the Theme type to makeStyles:

const useStyles = makeStyles((theme: Theme) => ({
    ...
}));

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
nareshbhatiacommented, Apr 19, 2019

Curious, why is https://unpkg.com/@material-ui/core@4.0.0-alpha.8/Select/SelectInput.d.ts defines onChange as

onChange?: (
    event: React.ChangeEvent<{ name?: string; value: unknown }>,
    child: React.ReactNode,
  ) => void;

Why not just

  onChange?: (
    event: React.ChangeEvent<HTMLSelectElement>,
    child: React.ReactNode,
  ) => void;

If I patch SelectInput.d.ts with this definition, all problems go away. But I am sure there’s a reason for why it is this way!

3reactions
nareshbhatiacommented, Apr 19, 2019

@eps1lon, that was super helpful! I am now able to type this a lot better. One type cast needed, but I fully understand why.

// ChartHeader.tsx
export interface ChartHeaderProps {
    onTimePeriodChange: (event: React.ChangeEvent<{ value: unknown }>) => void;
}
// AnalyzePage.tsx (consumes ChartHeader)
const handleTimePeriodChange = (event: React.ChangeEvent<{ value: unknown }>) => {
    setTimePeriod(event.target.value as string);
};

Here’s the source for anyone who hits this issue in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript error with v4.0.0-alpha.8 #15400 - mui/material-ui
The following code worked perfectly fine in alpha.7, but is broken in alpha.8. Can't tell if it's React signatures that have changed or ......
Read more >
material-ui/core/CHANGELOG.md - UNPKG
69, - Fix integration issue with TypeScript 4.1 (#23692) @ldrick ... 3728, ### `@material-ui/core@v4.0.0-alpha.8`. 3729. 3730, #### Breaking Changes.
Read more >
jaybe-react-universal-componentv4 - npm package | Snyk
v4.0.0-alpha.8 ... For Typescript or environments without Babel, just copy what ... render : (props, module, isLoading, error) => <CustomComponent ...
Read more >
Error: TSError: Unable to compile TypeScript - Stack Overflow
I came here looking for a solution for a similar error when I updated my typescript version, Mine was not a new project,...
Read more >
CHANGELOG.old.md - mui/material-ui - Sourcegraph
Fix integration issue with TypeScript 4.1 (#23692) @ldrick; ⚛️ Fix two issues with React 17 (#22263 ... @material-ui/styles@v4.0.0-alpha.8.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found