Skip to content
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

[TypeScript] Multiple errors on TableCell components #13372

Closed
2 tasks done
JeanBeaurepaire opened this issue Oct 24, 2018 · 5 comments
Closed
2 tasks done

[TypeScript] Multiple errors on TableCell components #13372

JeanBeaurepaire opened this issue Oct 24, 2018 · 5 comments
Labels
component: table This is the name of the generic UI component, not the React module! typescript

Comments

@JeanBeaurepaire
Copy link
Contributor

JeanBeaurepaire commented Oct 24, 2018

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

No typescript errors.

Current Behavior

Hello, I'm having some typescript issues with TableCell props direction and sortDirection

TS2322: Type 'string' is not assignable to type 'SortDirection'.
TS2322: Type 'string' is not assignable to type 'asc' | 'desc'.

I experienced those errors on an other library and the fixed was by passing it with an Enum instead of a type for 'asc' or 'dsc'. Example :

export type SortDirection = Direction | false; export const enum Direction { ASC = 'asc', DSC='desc' }

Steps to Reproduce

I'm not able to reproduce on the codesanbox, it's weird.

Link : https://codesandbox.io/s/jz25ov7p5w

Context

I can't compile.

Your Environment

tsconfig.json and tslint.json files are in the codesanbox

Tech Version
Material-UI 3.1.1
React 16.5.2
Browser
TypeScript 3.1.3
etc.
@eps1lon
Copy link
Member

eps1lon commented Oct 24, 2018

You encountered typescripts type widening.

Possible fix:

interface State {
  order: 'asc' | 'desc'
  orderBy: string;
}

class Index extends React.Component<{}, State> {
  state: State = {
    order: "asc",
    orderBy: "calories"
  };
  ...
}

codesandbox' typechecking is currently not working. That's why you couldn't reproduce this.

@JeanBeaurepaire
Copy link
Contributor Author

Ok thank you very much @eps1lon, it fixed my compile errors.

I used this example code:
https://github.com/mui-org/material-ui/blob/master/docs/src/pages/demos/tables/EnhancedTable.js

Should we fix it ? Is it a material-ui bug or a wront typescript implementation ?

@eps1lon
Copy link
Member

eps1lon commented Oct 24, 2018

Is the issue with types or at runtime?

You can't just simply copy code from javascript and call it typescript. At least not in most cases.

@oliviertassinari oliviertassinari added the component: table This is the name of the generic UI component, not the React module! label Oct 25, 2018
@JeanBeaurepaire
Copy link
Contributor Author

The issue is with types, I can't compile without your fix @eps1lon

@eps1lon
Copy link
Member

eps1lon commented Oct 25, 2018

Yes which is caused by typescript. I provided you with the proper documentation about that typescript behavior. It has nothing to do with this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: table This is the name of the generic UI component, not the React module! typescript
Projects
None yet
Development

No branches or pull requests

3 participants