简体   繁体   English

扩大打字稿中反应类型的问题

[英]Issue augmenting the react types in typescript

I'm trying to add the new hooks to react in typescript (last I checked, they weren't available on DefinitelyTyped), but it's not working. 我正在尝试添加新的钩子以在打字稿中做出反应(最后我检查了一下,它们在DefinitelyTyped上不可用),但是它不起作用。

I've made a file types/react.d.ts that looks like this: 我制作了一个文件types/react.d.ts ,如下所示:

import React from 'react';

declare module 'react' {
  function useState<T>(
    initialState: T | (() => T),
  ): [T, (newState: T | ((oldState: T) => T)) => void];
}

but when I try to use it, it's not working. 但是当我尝试使用它时,它不起作用。

在此处输入图片说明

You could refer to the Flow types for React Hooks here - https://github.com/facebook/flow/pull/7149/ 您可以在此处参考React Hooks的流类型-https: //github.com/facebook/flow/pull/7149/

They should be pretty similar to TypeScript. 它们应该与TypeScript非常相似。

In Flow, it looks like: 在Flow中,它看起来像:

declare type BasicStateAction<S> = (S => S) | S;
declare type Dispatch<A> = A => void;

declare export function useState<S>(
  initialState: (() => S) | S,
): [S, Dispatch<BasicStateAction<S>>];

事实证明,问题与Matt McCutchen建议的tsconfig.json配置有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM