chore: fix TypeScript types on constructor and get method #8
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "master"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Apologies, I should have picked these type issues up in my pull request yesterday.
Tvalues (otherwise it won't accept string keys)getmethod could return undefined.@ -1,7 +1,7 @@declare class OrderedMap<T = any> {constructor(content: T[])private constructor(content: Array<string | T>)Why
string | T?@ -1,7 +1,7 @@declare class OrderedMap<T = any> {constructor(content: T[])private constructor(content: Array<string | T>)@marijnh here's my thinking, apologies if I'm wrong.
If you say your OrderedMap contains numbers
OrderedMap<number>you then cannot initialize it with keysnew OrderedMap<number>([ 'key1', 1, 'key2', 2])without type errors.To get around this, you might try
OrderedMap<string | number>but that means ever time you do.get('key1')TypeScript thinks the result is number or string, so you need to do additional work to narrow the typeI added this as convince so you can pass keys in the constructor, without the need for narrowing later on
@ -1,7 +1,7 @@declare class OrderedMap<T = any> {constructor(content: T[])private constructor(content: Array<string | T>)As an aside, if the constructor took an array of arrays (like
Object.entriesgives) this would be easier to type.i.e.
new OrderedMap([ ['key1', 1], ['key2, 2] ])But obviously that would be a breaking change!!
@ -1,7 +1,7 @@declare class OrderedMap<T = any> {constructor(content: T[])private constructor(content: Array<string | T>)Ah, right, should have read a bit deeper. The thing is that only the documented part of the API is public, so this could just be
private constructor()@ -1,7 +1,7 @@declare class OrderedMap<T = any> {constructor(content: T[])private constructor(content: Array<string | T>)Yep makes sense. Thanks, will update.