使用react创建上下文时找不到命名空间'ctx'错误-Typescript
我工作的一个项目,react
使用typescript
,我有一个坏的时间弄清楚为什么这个错误发生的事情,我基本上不能使用任何createContext
的例子,我在互联网上找到,因为这一点。我具体是从这里获得的:https : //github /typescript-cheatsheets/react-typescript-cheatsheet我正在尝试使用“上下文”部分中显示的内容。
import * as React from "react"; export function createCtx(defaultValue: A) { type UpdateType = React.Dispatch; const defaultUpdate: UpdateType = () => defaultValue; const ctx = React.createContext({ state: defaultValue, update: defaultUpdate }); function Provider(props: React.PropsWithChildren) { const [state, update] = React.useState(defaultValue); return ; } return [ctx, Provider] as [typeof ctx, typeof Provider]; }
问题是,每次它说有此错误时,都无法ctx
在行中找到名称空间:
return ;
我仍然无法弄清楚为什么,有人可以看看我在这里做错了什么吗?这是我的tsconfig.json:
{ "compilerOptions": { "target": "es5", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": false, "forceConsistentCasingInFileNames": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "preserve", "noImplicitAny": false, "strictNullChecks": false }, "include": [ "src" ] }
任何帮助,将不胜感激!
1> lukasgeiter..:
您的文件扩展名很可能.ts
不是.tsx
。
因此,TypeScript被解释