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
It is recommended that TypeScript implementations provide a `createContext()` function which is used to create a `Context`. This function can just cast to a `Context`:
which allows taking an type argument for the context key. In order to take advantage of this though, both type arguments must be provided. If only ValueType is provided, K defaults to unknown as TypeScript does not do partial inference of type arguments.
Now in practice, I don't think having an explicit KeyType be provided to Context is quite necessary. The extraction of the ValueType does not require it:
I'm a bit torn as it feels correct to allow explicitly typing the KeyType as in the Lit implementation, but if it's functionally moot, that creates confusion like lit/lit#4601
One issue with having KeyType be unknown is that it can look confusing. With unknown & {__context: ValueType}, it just looks like {__context: ValueType} in type intellisense.
e.g.
The current recommendation for
createContext
hascommunity-protocols/proposals/context.md
Lines 114 to 121 in 952f158
This effectively makes
typeof key
to always beunknown
.Lit's implementation has
which allows taking an type argument for the context key. In order to take advantage of this though, both type arguments must be provided. If only
ValueType
is provided,K
defaults tounknown
as TypeScript does not do partial inference of type arguments.Now in practice, I don't think having an explicit
KeyType
be provided toContext
is quite necessary. The extraction of theValueType
does not require it:community-protocols/proposals/context.md
Lines 101 to 104 in 952f158
I'm a bit torn as it feels correct to allow explicitly typing the
KeyType
as in the Lit implementation, but if it's functionally moot, that creates confusion like lit/lit#4601One issue with having
KeyType
beunknown
is that it can look confusing. Withunknown & {__context: ValueType}
, it just looks like{__context: ValueType}
in type intellisense.e.g.
The text was updated successfully, but these errors were encountered: