Closed Gozala closed 1 year ago
It seems that Dict
type alias causes misbehavior, if I inline it it works as expected:
/* @flow */
const set = <a> (key:string, value:a, dict:{[key:string]:?a}):{[key:string]:?a} => {
dict[key] = value;
return dict
}
const get = <a> (key:string, dict:{[key:string]:?a}, fallback:a):a => {
const value = dict[key]
if (value != null) {
return value
} else {
return fallback
}
}
const d1 = set("a1", "Jack", {}) // I'd expect d1:Dict<string>
const v = get("a1", d1, 0) // I'd expect v:string|number
v.toFixed() // but flow inferst as v:number
and reports:
21: v.toFixed() // but flow inferst as v:number
^ property `toFixed`. Property not found in
21: v.toFixed() // but flow inferst as v:number
^ String
It seems that Dict type alias throws away what it knows about the property value type, and you could use a hack along this lines to stop it from throwing it away:
/* @flow */
type Dict <a> = {
[string]:?a,
value?: a
}
const set = <a> (key:string, value:a, dict:Dict<a>):Dict<a> => {
dict[key] = value;
return dict
}
const get = <a> (key:string, dict:Dict<a>, fallback:a):a => {
const value = dict[key]
if (value != null) {
return value
} else {
return fallback
}
}
const d1 = set("a1", "Jack", {}) // I'd expect d1:Dict<string>
const v = get("a1", d1, 0) // I'd expect v:string|number
v.toFixed()
/*
^ property `toFixed`. Property not found in
^ String
*/
This should be fixed now
Here is an example that illustrates the issue:
From what I can tell second call to
get
ignoring the fact thatd1
isDict<string>
or more likely set returnsDict<any>
and there forv
is inferred as number, completely ignoring a fact thatset
stored astring
there.