The Firebase typings have an interface IQuery
which has the equalTo()
method typed as:
public equalTo(value: number | string | boolean | null, name?: string): IQuery;
I have a mocking library which is trying to mimic the API surface of what Firebase is exposing but in the case of the name
parameter the general typing of "string" can be made more specific:
public equalTo(value: number | string | boolean | null, key?: keyof T): IQuery<T>
By stating keyof T
we get some very useful static checking that I'd hate to lose out on and in fact up to TypeScript 2.8.3 I seemed to be able to do this but when moving to 2.9.x
+ of Typescript I now get this error:
Type 'string' is not assignable to type 'keyof T'.
For a bit more context, the class definition in my mocking library is:
export default class Query<T = any> implements IQuery<T> { ... }
Where the <T>
generic type is an indicator of type of the data structure that the query will return.
Further it is worth noting that T, will always be shaped as a dictionary/hash with string keys and therefore keyof T
will subset of string values but in my typing I'm not making this clear so I get where Typescript is taking issue ... I just don't know how to express that keyof T
must be a string.