You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Seen on all versions. I have tested on: 4.4.0-dev.20210806 in playground
This is the behavior in every version I tried, and I reviewed the FAQ for entries about "This mapped type returns a primitive type, not an object type."
// create a getEnv function that will// return an object with key is supplied name// and value is a "computed/derived/other value", // typically process.env[name]constgetEnv=<Textendsstring>(...envNames: ReadonlyArray<T>): Record<T,string>=>{constnewObj=envNames.map(name=>{return{[name]: nameasstring};}).reduce((prev,curr)=>{return{ ...prev, ...curr};});// Bug: notice that "newObj" is not type Record<T, string> but Record<string, string>returnnewObj;};// @errors: expect that the "hello" variable here should result in a compilation failure, or "hello" should be "never"const{ myName, hello }=getEnv('myName');
🙁 Actual behavior
array.map().reduce() returns object with type Record<string,string>
🙂 Expected behavior
array.map().reduce() should return object with type Record<T,string> because the "key" in the map function is limited by value scope to the values in "T"
The text was updated successfully, but these errors were encountered:
This doesn't really have much to do with Array.map(). You can reproduce the behavior like this:
functionfoo<Kextendsstring>(k: K){consto={[k]: 123}// const o: { [x: string]: number; }// not { [P in K]: number }// or more accurately { [P in K]: { [Q in P]: number }}[K]};
Bug Report
🔎 Search Terms
array map reduce type inference
🕗 Version & Regression Information
Seen on all versions. I have tested on: 4.4.0-dev.20210806 in playground
⏯ Playground Link
Playground link with relevant code
)
💻 Code
🙁 Actual behavior
array.map().reduce() returns object with type Record<string,string>
🙂 Expected behavior
array.map().reduce() should return object with type Record<T,string> because the "key" in the map function is limited by value scope to the values in "T"
The text was updated successfully, but these errors were encountered: