-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Design Meeting Notes, 9/23/2016 #11104
Comments
What is the point of symbolic names for constants if they don't stick around in a |
The names stay, the question is about the values. If they are infected should be treated like the explicit ones, now that we are treating them differently. |
What I mean is, we've said we want to have literal types "stick" for constants because we believe that they are useful as symbolic variables for people to use. If the declaration emitter won't even respect that, there's no point to writing a library in that style because they'll just have to write |
But there is a difference, in the proposal above, between a const with no type annotation and one with a type annotation. |
Right, in the above proposal you widen for a constant, unless the type is already written out. But that removes the utility of even keeping string literals around for constants because you have to specifically write out the literal type again. You're not much better off than you were in the old contextual typing world. |
seems reasonable. |
Literal types -- cont'd
FAILURE
is a type that was explicitly declared, but assigning it to a let makes it disappear.Some setup:
every literal has a "domain", for instance
true
andfalse
have a domain, which isboolean
declaring
let x = true
intends to makex
go to the domain of hte literal type, which is booleansame goes for enums
the problem with string and numeric literal types, the domain is all strings or numbers, and there is no way to declare a subset of this domain
type Direction = "up" | "down" | "left" | "right"
declares a subdomain, yet when we widen we loose this information, and in a sense we do not distinguish values of this type from any other stringconst c3 = cond ? c1 : c2;
const c4: FooOrBar = cond c1 : c2;
let x = c3;
let y = c4;
what to do with declaration emitter
emits a declaration file as:
which is semantically different from the original, since the original has a fresh
"FAILURE"
type, that widens, but the second does not widen.this is only applicable for numeric and string literal types, and not enums or booleans.
so:
Options:
allow initialization in constant declarations in ambient context.. e.g.
Widening literal types for properties
null
andundefined
?ts let x = { type: "folder" }; let a: FolderContentItem = x; // Error today anyways
similarly a const would not work:
ts const x = { type: "folder" }; let a: FolderContentItem = x; // Still an error
Strict relationship for literal types (#10120)
<
and>
do not make much sense to be that strict. you should be able to compare1
>2
.===
and==
do make sense. we know thattrue === false
is guaranteed to fail all the time.proposal:
<
like operators, widen the literal types to thier base types before doing the check.The text was updated successfully, but these errors were encountered: