Skip to content

2.9 and 3.0 reduce type of optional branded string member as undefined #25709

@dcolthorp

Description

@dcolthorp

TypeScript Version: 3.0.0-dev.20180712, 3.0-rc, 2.9. Our original code worked fine in 2.8 with strictNullChecks enabled.

Search Terms: strictNullChecks, branding, enum, optional, intersection, undefined

Code

// strictNullChecks must be enabled to see this problem
enum Brand {}
type BrandedString = string & Brand;

export const fromString = (s:string) => s as BrandedString

interface ComplexType {
  optional?: BrandedString;
}
const example: ComplexType = {
  optional: fromString("foo")
};

Expected behavior: Example type checks

Actual behavior: Example does not type check TypeScript thinks optional is has type undefined.

Playground Link: Ensure strictNullChecks is enabled. Link here

Related Issues: #25179 looks similarish

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions