spotify-types icon indicating copy to clipboard operation
spotify-types copied to clipboard

Allow users to narrow down type

Open BasixKOR opened this issue 3 years ago • 1 comments

Hi! Thank you for creating a great package. However, while using it, I noticed I couldn't narrow down the object types using standard TS type guards. For example, given an item: Track | Episode; I can't narrow it down to Track by simply checking item.type === "episode".

This happens because all interfaces share the same SpotifyType, which doesn't guarantee an Episode will always have a "episode" as a type.

Could we change them to simple literal types to benefit from type narrowing? I'd like to file a PR if you're interested.

BasixKOR avatar Feb 06 '22 10:02 BasixKOR

I will work on this issue when i get some time. You can file a pr if you are interested too.

As for now, you can do it like this,

const track = item as Track;

scientific-dev avatar Feb 06 '22 11:02 scientific-dev