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
TypeScript outputs an object with string keys that can be parsed as numbers ({"1": T}) as an object with number keys when processed by an object map (output is {1: T} after object map).
When you do keyof on the object type with string keys after an object map, you get the keys back as strings as expected ("1"). However, if you keyof on the object type TypeScript outputs to a .d.ts file you get back a number (1).
I ran into this when moving some code between TypeScript projects. So it was consuming a generated .d.ts instead of the original type. So for some f(x) it works works as expected when x is in the same project as f but when f is in a different project to x there's an issue.
constsource={"1": 1,"2": 2,"3": 3};declarefunctionobjectMap<T>(object: T): {[KinkeyofT]: string};exportconstresult=objectMap(source);declarefunctionobjectKeys<T>(object: T): keyofT;// Type is `"1" | "2" | "3"`. Expected!exportconstkeys1=objectKeys(result);// This is the type you get for `result` when compiling to `.d.ts`.// (See `.d.ts` output for confirmation.)exportdeclareconstresultCopiedFromGeneratedDeclaration: {1: string;2: string;3: string;};// Type is `1 | 2 | 3`. Unexpectedexportconstkeys2=objectKeys(resultCopiedFromGeneratedDeclaration);
constsource={"1": 1,"2": 2,"3": 3};declarefunctionobjectMap<T>(object: T): {[KinkeyofT]: string};exportconstresult=objectMap(source);declarefunctionobjectKeys<T>(object: T): keyofT;// Type is `"1" | "2" | "3"`. Expected!exportconstkeys1=objectKeys(result);// This is the type you get for `result` when compiling to `.d.ts`.// (See `.d.ts` output for confirmation.)exportdeclareconstresultCopiedFromGeneratedDeclaration: {1: string;2: string;3: string;};// Type is `1 | 2 | 3`. Unexpectedexportconstkeys2=objectKeys(resultCopiedFromGeneratedDeclaration);
🙁 Actual behavior
Type of keys2 is 1 | 2 | 3.
🙂 Expected behavior
Type of keys2 is "1" | "2" | "3" since that's the same as if the parameter to objectKeys() was in the same project and not imported from a .d.ts file.
I’d also be happy with {[K in keyof T as ``${K}``]: number} forcing the mapped object to have string keys instead of number keys.
The text was updated successfully, but these errors were encountered:
Bug Report
TypeScript outputs an object with string keys that can be parsed as numbers (
{"1": T}
) as an object with number keys when processed by an object map (output is{1: T}
after object map).When you do
keyof
on the object type with string keys after an object map, you get the keys back as strings as expected ("1"
). However, if youkeyof
on the object type TypeScript outputs to a.d.ts
file you get back a number (1
).I ran into this when moving some code between TypeScript projects. So it was consuming a generated
.d.ts
instead of the original type. So for somef(x)
it works works as expected whenx
is in the same project asf
but whenf
is in a different project tox
there's an issue.(Playground)
🔎 Search Terms
Object map turns string keys into numbers
🕗 Version & Regression Information
4.8.4
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Type of
keys2
is1 | 2 | 3
.🙂 Expected behavior
Type of
keys2
is"1" | "2" | "3"
since that's the same as if the parameter toobjectKeys()
was in the same project and not imported from a.d.ts
file.I’d also be happy with
{[K in keyof T as ``${K}``]: number}
forcing the mapped object to have string keys instead of number keys.The text was updated successfully, but these errors were encountered: