Skip to content

Can't infer unique symbol types via another variable #35896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
falsandtru opened this issue Dec 28, 2019 · 4 comments · May be fixed by #55359
Open

Can't infer unique symbol types via another variable #35896

falsandtru opened this issue Dec 28, 2019 · 4 comments · May be fixed by #55359
Labels
Bug A bug in TypeScript
Milestone

Comments

@falsandtru
Copy link
Contributor

Literals such as 0 can be inferred correctly but unique symbols are not.

TypeScript Version: 3.7.x-dev.20191224

Search Terms:

Code

let revision: symbol;
const rev = revision = Symbol();
rev === revision;

Expected behavior:
Type rev is unique symbol.
Actual behavior:
Type rev is symbol. Can't even declare that rev is a unique symbol.
Playground Link: https://www.typescriptlang.org/play/index.html?ts=3.8.0-dev.20191224&ssl=1&ssc=1&pln=3&pc=18#code/DYUwLgBATiBuCWBneB7AdgLgogngWwCMVgBuAKAGN1FIZYIBeaOJVNRiAZXyOAAoAlOTqMGTOq3QkgA

Related Issues:

@jcalz
Copy link
Contributor

jcalz commented Dec 28, 2019

Just to make sure I understand. You're saying this works as you expect:

let wideNumber: number;
const narrowNumber = wideNumber = 0; 
// const narrowNumber: 0 👍

but this doesn't:

let wideSymbol: symbol;
const narrowSymbol = wideSymbol = Symbol();
// const narrowSymbol: symbol 😕

Similarly, this works as you expect:

const narrowNumber = 0;
// const narrowNumber: 0
const alsoNarrowNumber = narrowNumber;
// const alsoNarrowNumber: 0 👍

but this doesn't:

const narrowSymbol = Symbol();
// const narrowSymbol: typeof narrowSymbol
const alsoNarrowSymbol = narrowSymbol;
// const alsoNarrowSymbol: symbol 😕

Playground Link

Right?

@falsandtru
Copy link
Contributor Author

falsandtru commented Dec 29, 2019

const alsoNarrowSymbol = narrowSymbol;
// const alsoNarrowSymbol: symbol 😕

Not my intent but probably legal.

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Dec 29, 2019

Probably somewhat related. unique symbol has some funky widening rules

#32242

Even identity functions aren't safe from them. (<T>(t:T)=>T)

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 31, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 31, 2020
@coreh
Copy link

coreh commented Feb 20, 2020

Just hit a similar widening issue to this one and the one described by @AnyhowStep on #32242 (now closed). A unique symbol (typeof foo) becomes just symbol when returned from a generic function of the following signature:

function reduce<T>(fn: (value: T, acc: T) => T, initial: T): T {}

String literal types are not widened by this construct, but unique symbol is. 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
5 participants