Closed
Description
This may be intended, but I want a bug to track this in case others run into it.
// @strict: true
export interface Ref<T = any> { value: T }
type UnwrapRef<T> = T extends Ref<infer V> ? V : never
declare function readonly<T>(target: T): UnwrapRef<T>
const toReadonly = <T extends unknown>(value: T): T =>
// readonly(value as T & Record<any,any>)
isObject(value) ? readonly(value) : value
declare function isObject(val: unknown): val is Record<any, any>
This is an error without --strict
on 4.0, but not with --strict
. In 4.1, it's an error for both.
Expected behavior:
No error and value: Record<any, any>
in readonly(value)
.
Actual behavior:
Error and value: T & Record<any, any>
in readonly(value)
:
Type 'T | UnwrapRef<T & Record<any, any>>' is not assignable to type 'T'.
'T | UnwrapRef<T & Record<any, any>>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'.
Type 'UnwrapRef<T & Record<any, any>>' is not assignable to type 'T'.
'UnwrapRef<T & Record<any, any>>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'.
Type 'unknown' is not assignable to type 'T'.
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'.
Also I'm not sure where the last two lines of the error come from. They don't seem to follow from the previous lines.
Workaround
readonly(value as Record<any, any>)
to restore the old type of value
: casting to subtype is allowed.