Can't do a type assertion on an empty array with a branded type.
Bug Report
🔎 Search Terms
- type assertion
- branded type
- empty array
🕗 Version & Regression Information
Every version.
⏯ Playground Link
Playground link with relevant code
💻 Code
export type Foo = number & {__brand__: 'foo'};
const a = 3 as Foo; // OK
export type Bar = Array<number|string> &{ __brand__: 'bar'; }
const createOpCodes = [] as Bar; // KO
🙁 Actual behavior
const createOpCodes = [] as Bar;
throws :
Conversion of type 'never[]' to type 'Bar' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
🙂 Expected behavior
Should work fine without having to resort to a intermediate assertion like const createOpCodes = [] as unknown[] as Bar;
This follows pretty directly from the rules about which assertions are and aren't allowed. Is there something different about this case in particular, or are you just saying all assertions should always be allowed?
@RyanCavanaugh Perhaps if [] is being asserted, it should have the contextual type of unknown[] instead of never[] to allow it to be downcasted to a branded type?
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.