Closed
Description
TypeScript Version:
- typescript@2.8.4 ok
- typescript@2.9.2 bug
- typescript@3.0.0-dev.20180626 bug
Search Terms:
Code
declare const AddressSymbol: unique symbol;
type AddressString = typeof AddressSymbol & string;
function printout(data: AddressString | undefined) {
console.log(data); // type of `data` is always undefined since TS 2.9
}
printout(undefined); // ok
const a = "foo" as AddressString;
printout(a); // broken since TS 2.9
Expected behavior: the code compiles
Actual behavior: since TypeScript 2.9, the compilations fails with:
$ ./node_modules/typescript/bin/tsc example.ts
example.ts:11:10 - error TS2345: Argument of type 'AddressString' is not assignable to parameter of type 'undefined'.
11 printout(a); // broken in TS 2.9
~
Related Issues: #25179
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
ahejlsberg commentedon Jun 27, 2018
This is working as intended and is an effect of #23672. The type checker now more effectively removes intersection types with empty value domains (such as
symbol & string
) when they're included in union types. Since it isn't possible to construct values that are simultaneouslysymbol
andstring
, the typesymbol & string
is effectively the same asnever
and the type checker treats it accordingly.webmaster128 commentedon Jun 27, 2018
Thank you very much for the explanation, @ahejlsberg! Makes sense so far. We only cast to those things in order to get strict string aliases and they can indeed not be constructed.
But shouldn't
a
be of type never too in the following lines?