error when use in typescript
See original GitHub issueWhich OS ?
win7 64bit
Version
Which versions are you using:
- react-native-swiper V1.5.13
- react-native v0.49.3
- typescript v2.5.3
I got error when use with typescript:
Warning: React.createElement: type is invalid – expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it’s defined in.
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"jsx": "react",
"sourceMap": true,
"outDir": "./lib",
},
"include": ["./src/"]
}
when i write a simple component:
import * as React from 'react';
import Swiper from 'react-native-swiper'; // import default
import {View,Image} from 'react-native';
const Test =()=>(
<Swiper loop={false}>
<View >
<Image source={require(`../../images/test1.png`)}></Image>
</View>
<View >
<Image source={require(`../../images/test2.png`)}></Image>
</View>
</Swiper>
)
export default Test;
the result after tsc compile is:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const react_native_swiper_1 = require("react-native-swiper");
const react_native_1 = require("react-native");
const Test = () => (React.createElement(react_native_swiper_1.default, { loop: false },
React.createElement(react_native_1.View, null,
React.createElement(react_native_1.Image, { source: require(`../../images/test1.png`) })),
React.createElement(react_native_1.View, null,
React.createElement(react_native_1.Image, { source: require(`../../images/test2.png`) }))));
exports.default = Test;
//# sourceMappingURL=test.js.map
you can see that, in compiled js code use React.createElement(react_native_swiper_1.default, { loop: false }
create Swiper component,but actually react_native_swiper_1 is the valid component,while react_native_swiper_1.default is undefined,Because in index.js:
import Swiper from './src/'
module.exports = Swiper
I see the PR #393 resolved the problem. so why not use it?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Get a catch block error message with TypeScript - Kent C. Dodds
TypeScript forces you to acknowledge you can't know what was thrown making getting the error message a pain. Here's how you can manage...
Read more >Documentation - Understanding Errors - TypeScript
Whenever TypeScript finds an error, it tries to explain what went wrong in as much detail as possible. Because its type system is...
Read more >Improving TypeScript error handling with exhaustive type ...
In this article, we'll cover a few of the most common approaches for error handling in TypeScript (TS). We'll talk about most common...
Read more >Exception Handling - TypeScript Deep Dive - Gitbook
JavaScript has an Error class that you can use for exceptions. You throw an error with the throw keyword. You can catch it...
Read more >Type-Safe Error Handling In TypeScript - DEV Community
First, let's begin by acknowledging that throw is not typesafe. We must use a different approach in order to get the TypeScript compiler...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can just import from ‘react-native-swiper/src’ for now.
In node_modules/react-native-swiper has index.d.ts,but its version is old.When we use “npm install @types/react-native-swiper”, could get the latest d.ts. However our project with typescript only check the old d.ts, so please change the old d.ts with another name,not " index.d.ts",or update its type as the latest d.ts.