【问题标题】:TS2307 Error: Cannot find module '../constants' or its corresponding type declarationsTS2307 错误:找不到模块 '../constants' 或其对应的类型声明
【发布时间】:2021-11-23 12:14:48
【问题描述】:

我目前在尝试导入时遇到错误。任何帮助或建议将不胜感激。

tabs.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"
  ]
}

【问题讨论】:

    标签: javascript node.js reactjs typescript react-native


    【解决方案1】:

    该错误告诉您import 从它当前指向的位置没有任何内容。

    import-ing 从一个文件夹实际上是在该文件夹中寻找一个index.[d.](t|j)s 文件。如果该文件存在并且它具有默认导出,则可以在导入时为导出指定一个本地范围内的名称:

    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)
    

    更多示例here.
    规范here.

    如果您在/constants 文件夹中放置了一个名为icons.ts 且具有默认导出功能的文件,那么您的导入应该如下所示:

    import icons from '../constants/icons'
    // or, for a named export
    import { icons } from '../constants/icons'
    

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 2022-10-17
      • 2021-05-14
      • 2021-04-08
      • 2022-10-06
      • 2021-12-21
      • 2021-04-15
      • 2023-01-31
      • 2020-11-17
      相关资源
      最近更新 更多