-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
I'm not entirely sure why you would want to extend unknown
. But if you end up doing it, it seems a bit surprising that the narrowing behavior is not the same as for unknown
itself.
TypeScript Version: 4.0.0-dev.20200509
Search Terms: narrow unknown
Code
function works(arg: unknown): void {
typeof arg === "object" && !!arg && Object.keys(arg);
}
function fails<T extends unknown>(arg: T): void {
// No overload matches this call.
// Overload 1 of 2, '(o: {}): string[]', gave the following error.
// Argument of type 'T' is not assignable to parameter of type '{}'.
// Type 'unknown' is not assignable to type '{}'.
// Overload 2 of 2, '(o: object): string[]', gave the following error.
// Argument of type 'T' is not assignable to parameter of type 'object'.
// Type 'unknown' is not assignable to type 'object'.
typeof arg === "object" && !!arg && Object.keys(arg);
}
Expected behavior:
Both examples compile.
Actual behavior:
Only works
compiles.
Related Issues:
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug