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
{{ message }}
Repository navigation
string is not assignable to unbounded generic type T #18397
Actual behavior:
Ultimately, string is not assignable to type T on Typescript playground. On 2.4.2, I get string is not assignable to T | undefined. Both makes no sense...
That is the correct behavior. The interface declares a generic method getItem<T> method which returns a different type of value depending on what you pass to it. The object you are attempting to implement that interface with does not meet that requirement.
This is working as intended and is an effect of #16368. The type checker is pointing out that it is not safe to treat the type { getItem: (onComplete: OnComplete<string>) => Promise<string>; } as the more general type { getItem<T>(onComplete?: OnComplete<T>): Promise<T>; }. Basically, you can't treat a method that operates on the specific type string as a method that operates on any type T.
I suspect you probably want to write the code this way:
Also, for the longest time, I've been confused about what it means when <T> is on a method but not on the top-level interface. Seeing the amount of similar mistakes including for typings on React-Redux in the past, perhaps the team could consider augmenting the Typescript documentation on generics to explain this particular case as well?
Cliff Koh (@cliffkoh) Just FYI, the Storage type in that definition file is a mess. It has type parameters that are never used at all (which are pointless) and type parameters that never occur in a parameter position (which means you must always explicitly specify them).
Anders Hejlsberg (@ahejlsberg) Thanks for the FYI. Because of your FYI I took a deeper look and have updated the PR to hopefully addressed the concerns you expressed above (link to PR above), in the interests of the entire Typescript community having better typings to work with.
Activity
aluanhaddad commentedon Sep 11, 2017
That is the correct behavior. The interface declares a generic method
getItem<T>method which returns a different type of value depending on what you pass to it. The object you are attempting to implement that interface with does not meet that requirement.ahejlsberg commentedon Sep 11, 2017
This is working as intended and is an effect of #16368. The type checker is pointing out that it is not safe to treat the type
{ getItem: (onComplete: OnComplete<string>) => Promise<string>; }as the more general type{ getItem<T>(onComplete?: OnComplete<T>): Promise<T>; }. Basically, you can't treat a method that operates on the specific typestringas a method that operates on any typeT.I suspect you probably want to write the code this way:
cliffkoh commentedon Sep 11, 2017
Thank you for the explanation. I hit this problem with the definition of Storage as defined here: https://github.com/rt2zz/redux-persist/blob/master/type-definitions/index.d.ts
I will submit a PR for them to fix per your suggestion Anders Hejlsberg (@ahejlsberg).
Also, for the longest time, I've been confused about what it means when
<T>is on a method but not on the top-level interface. Seeing the amount of similar mistakes including for typings on React-Redux in the past, perhaps the team could consider augmenting the Typescript documentation on generics to explain this particular case as well?https://www.typescriptlang.org/docs/handbook/generics.html
Thank you again for the explanation.
ahejlsberg commentedon Sep 11, 2017
Cliff Koh (@cliffkoh) Just FYI, the
Storagetype in that definition file is a mess. It has type parameters that are never used at all (which are pointless) and type parameters that never occur in a parameter position (which means you must always explicitly specify them).cliffkoh commentedon Sep 13, 2017
Anders Hejlsberg (@ahejlsberg) Thanks for the FYI. Because of your FYI I took a deeper look and have updated the PR to hopefully addressed the concerns you expressed above (link to PR above), in the interests of the entire Typescript community having better typings to work with.
Mostly removed the use of generics bar the generics expressed in Mozilla's localForage library for their Storage API (under
LocalForageDbMethods): https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts