Suggestion: Type guard generic types
See original GitHub issueWould it be good to have type guarding on generic types? Or was this decided against?
Example 1
class Parent {
prop1: string;
}
class Child extends Parent {
childProp: string;
}
var t: Parent = new Child();
if (t instanceof Child) {
t.childProp; // no error
}
function myFunction<T extends Parent>(item: T) {
if (item instanceof Child) {
item.childProp; // error
}
}
I realize this example could be rewritten without generics. It’s just an example 😃
Example 2
Here’s a scenario I came across when creating a wrapper class:
interface NedbItem {
_id: string;
}
function isNedbItem(item: any): item is NedbItem {
return item != null && typeof item._id === "string";
}
class DatabaseWrapper<T> {
getItems() {
const itemsFromDatabase: T[] = .....;
itemsFromDatabase.forEach(i => this.stripNedbID(i));
return itemsFromDatabase;
}
private stripNedbID(item: T) {
if (isNedbItem(item))
{
delete item._id; // error, _id does not exist on T
}
}
}
Maybe a generic type with no constraint should be treated like an any
type in this case? (Once type guards are fixed for any types – See #4432)
This suggestion is probably very low on the priority scale.
I couldn’t find this issue discussed elsewhere so sorry if it was.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:18
- Comments:15 (6 by maintainers)
Top Results From Across the Web
TypeScript Generics and Type Guards - Explained by Example
This article describes how we leveraged two TypeScript features: generics and type guards in writing a robust utility function.
Read more >TypeScript Generics and Type Guards - Explained by Example
This article describes how we leveraged two TypeScript features: generics and type guards in writing a robust utility function.
Read more >typescript - How to add extends type guards to conditional ...
Let's say I have a class with a generic parameter. It has conditional types on some of the class' properties. These conditional types...
Read more >typing — Support for type hints — Python 3.11.1 documentation
This module provides runtime support for type hints. The most fundamental support consists of the types Any , Union , Callable , TypeVar...
Read more >generic-type-guard - npm
generic -type-guard works with the TypeScript type system. You are guaranteed that the type guards you write are sufficient to prove that the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It seems like it already sort-of works if I explicitly write a generic function that returns the type guard:
Unfortunately, it looks like narrowing of the union doesn’t work. If I add the explicit annotation
const data: Cat | Dog
the error goes away, so it looks like an unfortunate interaction between union narrowing and generics. In the meantime, we can just explicitly write if-clauses for each case instead of relying on narrowing.There are still valid cases where this can be helpful, especially when generic type extends from a union type. E.g
But the example doesnt seem to work today as typescript does not specialise the generic type even if it is a subset of the type it extends from