-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Design NotesNotes from our design meetingsNotes from our design meetings
Description
More Special Declaration Types in JS
In 2.7, we started treating variables initialized with empty object literals as namespaces in JS files.
var foo = {};
foo.x = 10
foo.y = 20;Now you can omit the var
app = {};
app.C = function() {
// ...
}Also allows IIFEs to be namespaces.
var C = (function () {
function C(n) {
this.p = n;
}
return C;
})();[[Daniel: I'm going to stop here because you can just read the PR writeup.]]
- We need to put these together in one document.
- Why do we do this in JavaScript and not in TypeScript?
- We're supposed to be a superset.
- What about declaration emit?
- Would we serialize using the TS-specific syntax?
- Should we only recognize this with
vardeclarations as opposed to justconsts?- Can always re-assign to a
var.
- Can always re-assign to a
- It is currently still awkward to transition from a
.jsfile to a.tsfile.- You switch to
.tsand you become temporarily untyped even if you want - What about a mode that enables these constructs in
.ts? - What about if
allowJsjust permitted this?- How do you disable the behavior though?
- When do you want to disable it?
- When you want to start writing super-strict
.tsin existing JS code.
- When you want to start writing super-strict
- When do you want to disable it?
- How do you disable the behavior though?
- "I'm worried about having too many flags"
- [[laughter]]
- You switch to
- What about initializing fields in a class?
- TypeScript requires fields to be declared up-front
- How do you prevent misspelled properties?
- Conclusion: we will think harder!
The unknown type
- Prototyped it as a new primitive type.
- Doesn't have to be a new primitive type.
- In non-strict mode, everything is assignable to
{}except forvoid. - In
strictNullChecks, you get a problem because you have to write{} | null | undefined; it's annoying. - Can just write an alias!
- That way you can still get narrowing; check
!= nulland then you'll be able to calltoString()on it.
- That way you can still get narrowing; check
- In non-strict mode, everything is assignable to
- Problem: If we put this in
lib.d.ts, you can run withskipLibCheckand TypeScript won't always print that out because it won't have seen the alias.- We could fix this in
strictNullChecksmode by forcing the checker to resolve this. - But then in non-
strictNullChecksmode,
- We could fix this in
- What about failed type argument inference?
- Could default to
unknowninstead of{}? - But not clear you get a lot of value from that.
- Could also not do anything.
- Problem: implies you'd need to change the constraint; implementations of generic functions currently make the assumption that type parameters will not be
nullorundefined(constraint is implicitly{}).
- Could default to
- We want it highlighted as a keyword though...
- So should it be an alias?
- Open questions
- Does
x > 0imply narrowing?- No.
if (x)narrows to{}?- Yes.
if (!x)narrows to{} | null | undefined?- Yes.
- Currently no?
- @chancancode reported this and we said "ehhhh"
- What does
{ [s: string]: unknown }mean?- Problems with dictionaries becoming assignable to anything.
- What's the type of
unknown | T?- Question becomes "should we collapse unions containing
{}?- Probably yes?
- But then you lose the contextual type in some cases, which would break a bunch of React code.
- We need to see what people are doing on DefinitelyTyped and see what the intent is there.
- Should write a TSLint rule to track them down.
- We need to see what people are doing on DefinitelyTyped and see what the intent is there.
- Question becomes "should we collapse unions containing
- What's the type of
s!?{}- (as opposed to
object | string | number | boolean).
- Does
Distribute keyof on intersections
This now works as expected!
type X<A, B> = Record<A, object> & Record<B, object>
// keyof A | keyof B
type Y<A, B> = keyof X<A, B>Metadata
Metadata
Assignees
Labels
Design NotesNotes from our design meetingsNotes from our design meetings