我试图通过使用Font.loadAsync导入一些字体,但是我得到了这个错误:
Unsafe assignment of an 'any' value

依赖于我的package.json
"dependencies": {
"@react-navigation/native": "^6.0.10",
"@react-navigation/native-stack": "^6.6.1",
"expo": "~44.0.0",
"expo-app-loading": "~1.3.0",
"expo-font": "~10.0.4",
"expo-linear-gradient": "~11.0.3",
"expo-status-bar": "~1.2.0",
"expo-updates": "~0.11.6",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-safe-area-context": "3.3.2",
"react-native-screens": "~3.10.1",
"react-native-snap-carousel": "^3.9.1",
"react-native-svg": "12.1.1",
"react-native-web": "0.17.1"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@types/react": "~17.0.21",
"@types/react-native": "~0.64.12",
"@types/react-native-snap-carousel": "^3.8.5",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
"babel-plugin-module-resolver": "^4.1.0",
"eslint": "^8.12.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.4.0",
"expo-cli": "^5.3.0",
"husky": "^7.0.4",
"lint-staged": "^12.3.7",
"typescript": "~4.3.5"
},我的问题是如何定义这些进口产品的类型?我已经创建了一个font.d.ts文件来定义*.otf,但是我仍然得到了这个错误。
发布于 2022-04-25 18:34:38
解决了
我发现这些导入的类型是FontSource,我所做的就是在我为这些字体创建的d.ts中声明这种类型:
declare module '*.otf' {
import { FontSource } from 'expo-font';
const value: FontSource;
export default value;
}此外,我还将导入方法更改为
import quicksandRegular from 'assets/fonts/Quicksand_Book.otf';
import quicksandBold from 'assets/fonts/Quicksand_Bold.otf';
import quicksandLight from 'assets/fonts/Quicksand_Light.otf';
function loadFonts() {
return Font.loadAsync({
'quicksand-regular': quicksandRegular,
'quicksand-bold': quicksandBold,
'quicksand-light': quicksandLight,
});
}这意味着我使用.otf语法进行的所有ES6导入都得到了ES6的定义。
https://stackoverflow.com/questions/72003991
复制相似问题