在反应组件中,通常你不应该在其中改变道具.此外,父母只能改变道具,而不能直接改变状态.基于这两个事实,假设在componentDidUpdate的任何调用中是正确的,例如,
componentDidUpdate(prevProps:Readonly>,prevState:Readonly)
this.props可能与prevProps不同,或者this.state可能与prevState不同,但这两种情况不能同时发生?
在打字稿中,我可以这样声明一个泛型函数:
const fn: <T>(arg: T)=>Partial<T>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,TypeScript有时可以根据我传递给它的实际参数来推断函数的类型参数。有没有类似的方法来定义可以根据其内容动态推断其类型参数的泛型对象文字?就像是:
interface XYZ {
obj: <T>{ arr: T[], dict: Partial<T> }
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使整个接口通用,如下所示:
interface XYZ<T> {
arr: T[],
dict: Partial<T>
}
Run Code Online (Sandbox Code Playgroud)
但我想避免这种情况,因为那时我每次使用接口时都必须提前声明泛型。例如
const x: XYZ
Run Code Online (Sandbox Code Playgroud)
不管用。如果要使该声明具有一般性,则必须写:
const x: XYZ<any>
Run Code Online (Sandbox Code Playgroud)
但这不允许TypeScript根据以下内容的实际内容动态推断特定的泛型类型: x
在编辑该项目时,是否有一种方法可以强制Visual Studio Code使用本地安装在JS项目中的TypeScript(而不是与VSCode捆绑在一起的版本或全局安装的版本)进行类型检查?
我在TypeScript中有这个简单的代码:
abstract class Config {
readonly NAME: string;
readonly TITLE: string;
static CoreInterface: () => any
}
class Test implements Config {
readonly NAME: string;
readonly TITLE: string;
}
Run Code Online (Sandbox Code Playgroud)
即使Test类中缺少CoreInterface()成员,TypeScript也不会抱怨.为什么是这样?
我需要每个派生类在CoreInterface()静态函数中提供有关自身的一些元数据.我知道我可以扩展Config类并让每个子类提供自己的CoreInterface()实现,但我不希望子类自动继承COnfig类的任何成员.这就是为什么我使用"implements"而不是"extends"
javascript ×3
typescript ×3
components ×1
generics ×1
interface ×1
object ×1
reactjs ×1
static ×1
types ×1