-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
TypeScript Version: 2.7.2 & 2.8.0-insiders.20180320
Search Terms:
Description
For a generic (that is being mapped with Readonly<>), it's properties are being narrowed to the value that is assigned as a default during destructuring with strictNullChecks set to true. I have seen this for boolean and enum types.
Code
interface Props {
foo?: boolean;
}
class Foo<P extends Props = Props> {
props: Readonly<P> = {} as P;
myFunc() {
const { foo = false } = this.props;
// errors because the type of `foo` is narrowed to `false` and cannot be
// compared to `true`
if (foo === true) {}
}
}Removing Readonly from the type of props "fixes" the error, so it seems like it is related to mapped types.
Expected behavior:
foo is narrowed to the defaulted value false but should keep the type boolean.
Actual behavior:
foo should default to false but keep the base type of boolean.
Related Issues:
SlurpTheo
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue