Typescript – How to fix errors?

How to check TypeScript in project folders?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ yarn tsc
$ yarn tsc
$ yarn tsc

Remember to exclude node_modules folders by configuring

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"compilerOptions": {
...
"skipLibCheck": true,
},
...
}
{ "compilerOptions": { ... "skipLibCheck": true, }, ... }
{
  "compilerOptions": {
      ...
      "skipLibCheck": true,
  },
  ...
}

Could not find a declaration file for module ‘third-party-library-name’. ‘third-party-library-name.js’ has an ‘any’ type.

  • Create “types\third-party-library-name\index.d.ts” file
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
declare module 'third-party-library-name';
declare module 'third-party-library-name';
declare module 'third-party-library-name';
  • Edit tsconfig.json
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"compilerOptions": {
...
"typeRoots": ["./types", "node_modules/@types"]
},
"exclude": [
...
"types"
]
"compilerOptions": { ... "typeRoots": ["./types", "node_modules/@types"] }, "exclude": [ ... "types" ]
"compilerOptions": {
    ...
    "typeRoots": ["./types", "node_modules/@types"]
},
"exclude": [
    ...
    "types"
]

How to define type for react-native style?

  • Use StyleProp<ViewStyle>, StyleProp<TextStyle>, StyleProp<ImageStyle>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from 'react';
import { View, StyleProp, ViewStyle } from 'react-native';
interface Props {
style: StyleProp<ViewStyle>;
}
const Empty = ({ style }: Props) => {
return <View style={style} />;
};
export default Empty;
import React from 'react'; import { View, StyleProp, ViewStyle } from 'react-native'; interface Props { style: StyleProp<ViewStyle>; } const Empty = ({ style }: Props) => { return <View style={style} />; }; export default Empty;
import React from 'react';
import { View, StyleProp, ViewStyle } from 'react-native';

interface Props {
  style: StyleProp<ViewStyle>;
}

const Empty = ({ style }: Props) => {
  return <View style={style} />;
};

export default Empty;

How to define type for react-native children prop?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
...
const MyComponent: React.FC = ({ children }) => {
return <StyledText>{children}</StyledText>;
};
export default MyComponent;
... const MyComponent: React.FC = ({ children }) => { return <StyledText>{children}</StyledText>; }; export default MyComponent;
...
const MyComponent: React.FC = ({ children }) => {
  return <StyledText>{children}</StyledText>;
};

export default MyComponent;

How to define type for styled-components?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
interface StyledProps {
readonly isAvailable: boolean;
}
const StyledContainer = styled(Card).attrs(() => ({}))<StyledProps>`
border-radius: 6px;
padding: 10px 10px;
border-bottom-width: 4px;
border-bottom-color: ${(props) =>
props.isAvailable ? '#c0e43e' : '#be1e2b'};
background-color: #f9f9fc;
`;
interface StyledProps { readonly isAvailable: boolean; } const StyledContainer = styled(Card).attrs(() => ({}))<StyledProps>` border-radius: 6px; padding: 10px 10px; border-bottom-width: 4px; border-bottom-color: ${(props) => props.isAvailable ? '#c0e43e' : '#be1e2b'}; background-color: #f9f9fc; `;
interface StyledProps {
  readonly isAvailable: boolean;
}

const StyledContainer = styled(Card).attrs(() => ({}))<StyledProps>`
  border-radius: 6px;
  padding: 10px 10px;
  border-bottom-width: 4px;
  border-bottom-color: ${(props) =>
    props.isAvailable ? '#c0e43e' : '#be1e2b'};
  background-color: #f9f9fc;
`;

Be the first to comment

Leave a Reply

Your email address will not be published.


*