一个代码块可绘制1000个单词:
我无法理解的例子。
对于mySomething
的预期参数,mySet()
为什么无效?
看来mySomething
应该是“ my
和mySet
上联合类型的一部分的”交集类型”。
我还尝试为info
创建类型,而不是T extends 'a'|'b'|'c'|'d'
,而是使用T extends keyof InfoType
,但结果相同。
我肯定会得到这一天,非常感谢。
type Something = Map<string,boolean> | Map<string,number>
const [a, setA] = useState(new Map<string,boolean>());
const [b, setB] = useState(new Map<string,boolean>());
const [c, setC] = useState(new Map<string,number>());
const [d, setD] = useState(new Map<string,number>());
const info = {
a: [a, setA] as [Map<string,boolean>,React.Dispatch<React.SetStateAction<Map<string, boolean>>>],
b: [b, setB] as [Map<string,boolean>,React.Dispatch<React.SetStateAction<Map<string, boolean>>>],
c: [c, setC] as [Map<string,number>,React.Dispatch<React.SetStateAction<Map<string, number>>>],
d: [d, setD] as [Map<string,number>,React.Dispatch<React.SetStateAction<Map<string, number>>>],
}
const f = <T extends 'a'|'b'|'c'|'d'>(t: T) => {
const [my, mySet] = info[t];
const mySomething = (t === 'a'||t==='b'? new Map<string,boolean>():new Map<string,number>()) // tried also with: as Something;
mySet(mySomething);
mySet(my); // <- this also has the same error
};
错误:
const mySomething: Something
Argument of type 'Something' is not assignable to parameter of type '(Map<string, boolean> & Map<string, number>) | (Map<string, boolean> & ((prevState: Map<string, number>) => Map<string, number>)) | (((prevState: Map<string, boolean>) => Map<...>) & Map<...>) | (((prevState: Map<...>) => Map<...>) & ((prevState: Map<...>) => Map<...>))'.
Type 'Map<string, boolean>' is not assignable to type '(Map<string, boolean> & Map<string, number>) | (Map<string, boolean> & ((prevState: Map<string, number>) => Map<string, number>)) | (((prevState: Map<string, boolean>) => Map<...>) & Map<...>) | (((prevState: Map<...>) => Map<...>) & ((prevState: Map<...>) => Map<...>))'.
Type 'Map<string, boolean>' is not assignable to type '((prevState: Map<string, boolean>) => Map<string, boolean>) & Map<string, number>'.
Type 'Map<string, boolean>' is not assignable to type '(prevState: Map<string, boolean>) => Map<string, boolean>'.
Type 'Map<string, boolean>' provides no match for the signature '(prevState: Map<string, boolean>): Map<string, boolean>'.ts(2345)