![](/img/trans.png)
[英]React,Storybook - TS2307: Cannot find module 'Button' or its corresponding type declarations CAN SB RESOLVE?
[英]TS2307 Error: Cannot find module '../constants' or its corresponding type declarations
我目前在嘗試導入時遇到錯誤。 任何幫助或建議將不勝感激。
標簽.tsx | src>導航>tabs.tsx
import React from 'react'
import { StyleSheet, View, Image, Text } from 'react-native'
import {createBottomTabNavigator, BottomTabBar} from "@react-navigation/bottom-tabs"
import {icons} from '../constants'
const Tab = createBottomTabNavigator()
結構
tsconfig.json
{
"compilerOptions": {
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es2017"], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"jsx": "react-native", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"noEmit": true, /* Do not emit outputs. */
"isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
"strict": true, /* Enable all strict type-checking options. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"skipLibCheck": false, /* Skip type checking of declaration files. */
"resolveJsonModule": true /* Allows importing modules with a ‘.json’ extension, which is a common practice in node projects. */
},
"exclude": [
"node_modules", "babel.config.js", "metro.config.js", "jest.config.js"
]
}
該錯誤告訴您沒有任何東西可以從它當前指向的位置import
。
從文件夾import
-ing 實際上是在該文件夾中查找index.[d.](t|j)s
文件。 如果該文件存在並且具有默認導出,您可以在導入時在本地 scope 中為導出分配一個名稱:
import stuff from '../constants'
如果它包含命名導出,您可以使用括號導入它們:
import { myExport } from '../constants' //or
import { myExport as localName } from '../constants'
注意:以上暗示您的index.ts
包含類似以下內容:
export const myExport = (/* some expression */)
// you can export types, consts, functions, classes, interfaces, etc...
如果要從該文件導入所有命名導出,請使用*
:
import * from '../constants' // myExport is now usable
// or namespaced:
import * as local from '../constants' // (and use as local.myExport)
如果您在/constants
文件夾中放置了一個名為icons.ts
具有默認導出的文件,您的導入應如下所示:
import icons from '../constants/icons'
// or, for a named export
import { icons } from '../constants/icons'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.