typescript - 正确键入 MobX 的 React/Typescript 输入组件
问题描述
我想用 Typescript for MobX 编写一个 React Input 组件,它作为它的输入道具:
- 一个mobx商店
stateObject
- 那家商店的钥匙
stateKey
打字稿应该强制它stateObject[stateKey]
是字符串类型的。也就是说,ExtraProps 应该在下面的示例中处理什么。但是,我无法让类型按照我期望的方式编译,但是得到
TS2322:类型 'string' 不能分配给类型 'K extends K ?字符串:任何'。
所以以下是我的方法:
import React, {ChangeEvent, Component, InputHTMLAttributes} from "react";
type ExtraProps<T extends string | number | symbol, K extends T> = {
stateKey: K,
stateObject: { [k in T]: k extends K ? string : any }
}
type InputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "value" | "onChange">
type InputPropType<T extends string | number | symbol, K extends T> = InputProps & ExtraProps<T, K>
export class InputField<T extends string | number | symbol, K extends T> extends Component<InputPropType<T, K>, any> {
constructor(props: InputPropType<T, K>) {
super(props);
this.onChange = this.onChange.bind(this)
}
onChange(event: ChangeEvent<HTMLInputElement>) {
let val: K extends K ? string : any = event.target.value; // TS2322: Type 'string' is not assignable to type 'K extends K ? string : any'.
this.props.stateObject[this.props.stateKey] = val;
}
render() {
let {stateKey, stateObject, ...props} = this.props;
return <input {...props} value={stateObject[stateKey]} onChange={this.onChange}/>
}
}
现在我的问题是:
- 我的类型有什么问题?
- 这甚至是实现我想要的明智的途径,还是有更好的方法?
同样,我想要的是一个托管输入组件,它只是从 mobx 存储读取/写入其值。
解决方案
你需要这样的东西:
// TypeScript Voodoo. See:
// https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c
// for details about how this works.
export type FilterFlags<Base, Condition> = { [Key in keyof Base]: Base[Key] extends Condition ? Key : never };
export type AllowedNames<Base, Condition> = FilterFlags<Base, Condition>[keyof Base];
用法是:
type ExtraProps<T> = AllowedNames<T, string>;
type InputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "value" | "onChange">
// not really sure about this, but I don't think the original '&' makes sense:
type InputPropType<T> = InputProps | ExtraProps<T>;
它为您提供了一个联合类型,其中包含 type 的字符串属性列表K
。您不需要 type K
,您只需要 type T
,这是您的stateObject
值的类型。
推荐阅读
- ios - BackBarButton 文本在取消隐藏后被截断
- c# - 我的 Asp.net 核心应用程序事件处理程序不起作用
- python - 将数据传递给 tkinter dnd (dragndrop) vs 事件绑定问题
- reactjs - React - 提交表单时如何使api请求等待未定义变量的值
- flutter - Dartdoc -> 排除方法体
- html - 想知道是否有办法找到与渲染相关的代码
- javascript - 如何从 javascript 中捕获 Calendly iframe 重新安排事件
- c# - 如何在 Unity3D 中更改具有多个材质的对象上的单个材质?
- typescript - 赛普拉斯:无法与页面元素交互
- python - 为什么 MIN() 和 MAX() 等原生 CQL 函数在 Amazon Keyspaces 中不起作用?