当我启用noImplicitThis时tsconfig.json,我得到以下代码的此错误:
Run Code Online (Sandbox Code Playgroud)'this' implicitly has type 'any' because it does not have a type annotation.
class Foo implements EventEmitter {
on(name: string, fn: Function) { }
emit(name: string) { }
}
const foo = new Foo();
foo.on('error', function(err: any) {
console.log(err);
this.emit('end'); // error: `this` implicitly has type `any`
});
Run Code Online (Sandbox Code Playgroud)
将typed添加this到回调参数会导致相同的错误:
foo.on('error', (this: Foo, err: any) => { // error: `this` implicitly has type `any`
Run Code Online (Sandbox Code Playgroud)
解决方法是替换this为对象:
foo.on('error', (err: any) => {
console.log(err);
foo.emit('end'); …Run Code Online (Sandbox Code Playgroud) 在测试我的UserRouter时,我使用的是json文件
data.json
[
{
"id": 1,
"name": "Luke Cage",
"aliases": ["Carl Lucas", "Power Man", "Mr. Bulletproof", "Hero for Hire"],
"occupation": "bartender",
"gender": "male",
"height": {
"ft": 6,
"in": 3
},
"hair": "bald",
"eyes": "brown",
"powers": [
"strength",
"durability",
"healing"
]
},
{
...
}
]
Run Code Online (Sandbox Code Playgroud)
构建我的应用程序,我得到以下TS错误
ERROR in ...../UserRouter.ts
(30,27): error TS7006: Parameter 'user' implicitly has an 'any' type.
Run Code Online (Sandbox Code Playgroud)
UserRouter.ts
import {Router, Request, Response, NextFunction} from 'express';
const Users = require('../data');
export class UserRouter {
router: Router;
constructor() {
... …Run Code Online (Sandbox Code Playgroud) 我是本机反应的新手。我正在尝试使用 react-navigation 5 实现导航。我有两个屏幕主页和个人资料。这两个组件都收到了导航道具,但打字稿给了我以下错误
Binding element 'navigation' implicitly has an 'any' type.ts(7031)
Run Code Online (Sandbox Code Playgroud)
如何摆脱这个错误。
先感谢您
注意 - 我正在使用打字稿
应用程序.tsx
import 'react-native-gesture-handler';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();
import Home from './screens/home'
const App = () => {
const ProfileScreen = ({}) => {
return <Text>This is Jane's profile</Text>;
};
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Home}
/>
<Stack.Screen name="Profile" component={ProfileScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
Run Code Online (Sandbox Code Playgroud)
主页.tsx
import React from 'react';
import …Run Code Online (Sandbox Code Playgroud)