Javascript 为typescript键入safe Immutable.Js getIn
有没有人能够为Immutable Map的getIn函数创建类型安全的类型 下面是我为it创建类型的尝试,以便它能够使用递归不可变映射Javascript 为typescript键入safe Immutable.Js getIn,javascript,typescript,immutable.js,Javascript,Typescript,Immutable.js,有没有人能够为Immutable Map的getIn函数创建类型安全的类型 下面是我为it创建类型的尝试,以便它能够使用递归不可变映射 import * as Immutable from 'immutable' type ImmutableType = '0' type PartialType = '1' type PrimitiveType = '2' type SchemaTypes = ImmutableType | PartialType | PrimitiveType type
import * as Immutable from 'immutable'
type ImmutableType = '0'
type PartialType = '1'
type PrimitiveType = '2'
type SchemaTypes = ImmutableType | PartialType | PrimitiveType
type Switch<Type extends SchemaTypes, K extends Schema<Type>> = {
0: ImmutableExtra<K> & Immutable.Map<keyof K, any>
1: Partial<K>;
2: K
}[Type];
type ImmutableParent = Switch<ImmutableType, ParentSchema<ImmutableType>>
let immutableParent: ImmutableParent
const childStringViaGet = immutableParent.get("child").get("childString") //typesafe. type is string
//Can't figure out this part
const childStringViaGetIn = immutableParent.getIn(["child", "childString"]) //not typesafe. type is any. Can't figure it out
interface Schema<T extends SchemaTypes> {
}
export interface ImmutableExtra<T extends Schema<ImmutableType>> {
get<K extends keyof T>(value: K): T[K]
getIn<A extends keyof T>(keys: [A]): T[A]
// Things I tried but doesn't work
// getIn<A extends keyof T, B ChildImmutable extends T[A]>(keys: [A, B]): T[A]["get"]
//getIn<A extends keyof T, B extends keyof T[A]>(keys: [A]): T[A][B]
//getIn<A extends keyof T, B extends keyof ChildType, ChildType, ChildImmutable extends T[A] & ImmutableExtra<ChildType>>(keys: [A, B])
}
interface ParentSchema<T extends SchemaTypes> {
parentString: string
parentNumber: number
child: Switch<T, ChildSchema<T>>
}
interface ChildSchema<T extends SchemaTypes> {
childString: string
childNumber: number
}
import*作为“不可变”的不可变项
类型ImmutableType='0'
类型PartialType='1'
类型原语类型='2'
类型SchemaTypes=ImmutableType | PartialType | PrimitiveType
类型开关={
0:ImmutableExtra和Immutable.Map
1:部分;
2:K
}[类型];
类型ImmutableParent=开关
让immutableParent:immutableParent
const childStringViaGet=immutableParent.get(“child”).get(“childString”)//类型安全。类型是字符串
//我搞不懂这部分
const childStringViaGetIn=immutableParent.getIn([“child”,“childString”])//不是类型安全的。类型是任意的。搞不懂
接口模式{
}
导出接口ImmutableExtra{
get(值:K):T[K]
输入(键:[A]):T[A]
//我试过但没用的东西
//getIn(键:[A,B]):T[A][“get”]
//输入(键:[A]):T[A][B]
//进入(键:[A,B])
}
接口父模式{
父字符串:字符串
家长号码:号码
孩子:开关
}
接口子模式{
childString:string
儿童号码
}
显然,这在一般情况下目前是不可能的,但您可能会在几个元组长度中重载它。请参见此处的相关讨论:
显然,这在一般情况下目前是不可能的,但是您可以在几个元组长度上重载它。请参见此处的相关讨论:
我在网上还看不到一个好的答案,但是如果你想获得更多的信息,人们在网上也有想法和部分实施,我在网上还看不到一个好的答案,但是如果你想获得更多的信息,人们在网上也有想法和部分实施