[英]ESLint issue when using babel module-resolver and typescript
TS is not inferring the type when using resolved imports, but it does when using relative imports. TS 在使用解析导入时不推断类型,但在使用相对导入时会推断类型。 Any help would be appreciated.
任何帮助,将不胜感激。
useTheme has使用主题有
"Unsafe call of an 'any' typed value."
“对‘任意’类型值的不安全调用。”
This error does not show up if i use a relative import instead of '@theme/Theme'
如果我使用相对导入而不是
'@theme/Theme'
,则不会出现此错误
My project structure is:我的项目结构是:
Here are my config files:这是我的配置文件:
tsconfig.json tsconfig.json
{
"extends": "../../tsconfig.json", // standard tsconfig params
"compilerOptions": {
"jsx": "react-native" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
"baseUrl": "." /* Base directory to resolve non-absolute module names. */,
"paths": {
"@components/*": ["./src/common/components/*"],
"@theme/*": ["./src/lib/theme/*"],
"@hooks/*": ["./src/common/hooks/*"],
"@features/*": ["./src/features/*"]
},
"typeRoots": [
"./typings",
"../../node_modules/@types"
] /* List of folders to include type definitions from. */
},
"include": ["../"],
"exclude": [
"node_modules",
"babel.config.js",
"metro.config.js",
"jest.config.js"
]
}
babel.config.js babel.config.js
module.exports = {
presets: [
'module:metro-react-native-babel-preset',
'@babel/preset-typescript',
],
plugins: [
['@babel/plugin-proposal-optional-chaining'],
['@babel/plugin-proposal-nullish-coalescing-operator'],
['@babel/plugin-proposal-decorators', { legacy: true }],
[
'module:react-native-dotenv',
{
moduleName: '@env',
path: '.env',
},
],
[
'module-resolver',
{
alias: {
'@components': './src/common/components',
'@theme': ['./src/lib/theme'],
'@hooks': ['./src/common/hooks'],
'@features': ['./src/features'],
},
extensions: ['.js', '.jsx', '.ts', '.tsx'],
root: '.',
},
],
'react-native-reanimated/plugin',
],
};
.eslintrc .eslintrc
{
"root": true,
"extends": [
"airbnb-typescript", // React "airbnb-typescript/base" for just js. "airbnb","airbnb/base" for no TypeScript
"airbnb/hooks", // React
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"@react-native-community" // React Native only
],
"parser": "@typescript-eslint/parser", // TypeScript only
"parserOptions": {
"project": "./tsconfig.json", // TypeScript only
"ecmaFeatures": { "jsx": true }
},
"settings": {
"import/resolver": {
"babel-module": {}
}
},
"plugins": ["import", "@typescript-eslint"],
"ignorePatterns": ["*.config.js"],
"overrides": [
{
/** Testing Files Extensions */
"files": ["*.{spec,test}.{js,ts,tsx}", "e2e/*.{js,ts,tsx}"],
"plugins": ["jest"],
"env": { "jest/globals": true },
"rules": {
"import/no-extraneous-dependencies": 0 // Allow dev-dependency imports
}
}
],
"rules": {
"no-unused-vars": [
"error",
{
"args": "none",
"caughtErrors": "none",
"ignoreRestSiblings": true,
"vars": "all"
}
],
// resolve "import/extensions"
"import/extensions": [
"error",
"ignorePackages",
{
"ts": "never",
"tsx": "never"
}
],
"curly": ["error", "multi-line"],
"no-void": ["error"], // Allow void as statement in order to ignore promise returns
"prettier/prettier": 0, // Disable Prettier
"import/no-cycle": 0, // Slows down all linting
"react/jsx-props-no-spreading": 0, // Enable Prop Spreading
"@typescript-eslint/no-empty-interface": 0, // Allow boilerplate empty interfaces for defining component props explicitly
"@typescript-eslint/no-unsafe-assignment": 0,
"@typescript-eslint/naming-convention": [
2,
{
"leadingUnderscore": "allow",
"format": ["camelCase", "PascalCase", "UPPER_CASE", "snake_case"],
"selector": "variable"
}
],
"@typescript-eslint/no-explicit-any": 0, // disabled to avoid using any type
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/restrict-template-expressions": 0,
"@typescript-eslint/no-floating-promises": [2, { "ignoreIIFE": true }], // Fixes issue where async await syntax wasn't being recognised.
"@typescript-eslint/explicit-function-return-type": [
0,
{ "allowExpressions": false }
],
"@typescript-eslint/no-unused-vars": [2, { "varsIgnorePattern": "^_" }],
// Fixes false-positives for enums in typescript https://github.com/typescript-eslint/typescript-eslint/issues/2484#issuecomment-687257773
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["error"],
"max-len": [
"warn",
{
"code": 120,
"tabWidth": 2,
"ignoreComments": true,
"ignoreStrings": true,
"ignoreRegExpLiterals": true,
"ignoreTemplateLiterals": true
}
]
}
}
if you work in vscode, try use "TypeScript: Select TypeScript version" to select you workspace version如果你在 vscode 中工作,请尝试使用“TypeScript:Select TypeScript 版本”到 select 你的工作区版本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.