-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Mapped TypesThe issue relates to mapped typesThe issue relates to mapped types
Milestone
Description
Assigning generic type T to it's deep readonly DeepReadonly<T> cause error however shallow Readonly<T> works fine. Also I found that known types do work well with DeepReadonly .
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
DeepReadonly assign return generic nested readonly
Code
// Simple deep readonly
export type DRO<T> = { readonly [P in keyof T]: DR<T[P]> };
type DR<T> = T extends object ? DRO<T> : T;
// Sample type
interface TMP {
a: number;
b: {
c: boolean;
};
}
// Works fine
const a: TMP = { a: 10, b: { c: false } };
const b: DRO<TMP> = a;
// Doesn't work
class X<T> {
constructor(private readonly t: T) {}
foo(): DRO<T> {
// Error here
return this.t;
}
bar(): Readonly<T> {
// Works fine
return this.t;
}
}Expected behavior:
Method foo shouldn't have error
Actual behavior:
Error: Type 'T' is not assignable to type 'DRO'.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Mapped TypesThe issue relates to mapped typesThe issue relates to mapped types