Repository navigation
Design Meeting Notes, 6/1/2018 #24593
Description
--strictAny
-
Talked about this last time, but no implementation.
-
Now we have an implementation and data.
-
Came from conversation with the Hack team.
- (which, funny, was inspired)
-
Background: problem today is that
anyis assignable in both directions.- Problem is often that a user wrote
anyin a declaration file because they were lazy or just scaffolding things.- But that means that any consumer of the
.d.tsfile has no way to defensively avoid assigninganyto anything else.
- But that means that any consumer of the
- This PR removes the "bottom-ness" from
any- makesanya top type.
- Problem is often that a user wrote
-
Tried running
--strictAnyon DefinitelyTyped-
Not on tests - tests have too much breakage.
-
Some things got broken - declaration files were
-
Much of the time it's just declaration authors being a little lazy.
- Instantiating generics with
any- But hey, even we're lazy! We instantiate the
prototypeproperty on generic classes withanyacross.
- But hey, even we're lazy! We instantiate the
- Using
anyin derived classes.
- Instantiating generics with
-
Why is
anynot assignable toRecord<string, any>>- Ehh, maybe it should be.
-
There are some issues with signatures like
(x: any, y: any, ...zs: any[]) => any.- Now
anyis subject to contravariant checking. - Again, can tell users to use
neverinstead ofany.
- Now
-
Issues with
SVGElementnot being assignable toElement.- DOM in general isn't sound, so... :(
-
Then there's the "covariant event emitter pattern".
once(event: string, listener: (...args: any[]) => void): this; once(event: "close", listener: () => void): this; once(event: "drain", listener: () => void): this; once(event: "error", listener: (err: Error) => void): this; once(event: "finish", listener: () => void): this; once(event: "pipe", listener: (src: Readable) => void): this; once(event: "unpipe", listener: (src: Readable) => void): this;
-
-
never?neverisn't very intuitive- We've tried to help users reason about it as a value that can never occur.
-
The hard-to-solve problems really come from
anyin contravariant positions. -
Tried running this on the compiler.
- Finds some questionable things.
commandLineParseris sloppy.- Calls to
Array()always return anany[].- Probably should move the generic overload to the top?
- Does that work right?
- Probably should move the generic overload to the top?
isArrayis a predicate onArray<{}>- Functions with overloads in derived classes with an
anyin the middle.- No.
- Did this catch any legitimate bugs?
commandLineParserhad a lot of questionable behavior.
- Also, like
prototypes of generic classes, there are some places whereanynever occurred but the type checker - for example, erased signatures.
-
Really don't like
as never- None of us does.
-
Really two usages of
anythat have been conflated:- I was lazy.
- "Ehh, shut up you stupid type-checker."
-
The answer may just be to tell people to go through their files and return
unknown. -
Have a list of places
anyjust appears:- Type parameters of prototype property in generic class
- String index signature in implied type of binding pattern (
function f({...x})) - Implied type of omitted element in array binding pattern (
function f([,,x])) - Catch clause variables
- Shorthand ambient modules
- Union signature with only some explicit 'this' types
- Types of properties in object from inference to 'keyof T'
- Type of 'super' in object literal expressions
- 'yield' expressions
-
Not sure about this feature.
- Part of the reason we went in this direction is because
anyhas become so conflated and prevalent.- Library authors don't always know how users intend to use these things.
- Part of the reason we went in this direction is because
-
Some users really want a more capable
unknown. -
Some users really want a
dynamicinstead of anany. -
What about per-file
strict-ness?- May cause a lot of user confusion.
-
If we did this all over, we could have considered
unknownto be treated as "effectivelyany" for usage, still not assignable. -
Can't imagine we would use this in
--strict -
Would we use this feature?
- Yes.
- But we're also the freaking TypeScript team.
- Yes.
-
We'd like to get feedback from users based on the current behavior and see if it's truly paletable.
"Loose mode" in .ts files vs. TypeScript in .js files
- Hard to reason about the semantics
- Webpack migration to
checkJshas been instructive- But we're not trying to solve that in these proposals.
- Two things in
checkJsthat make things different.- JSDoc comments
- Change how we check things.
- So maybe instead of a "loose mode", it's more like
allowJsDoc.- Also errors when your JSDoc and TypeScript annotations disagree! :D
- So is tooling not enough here?
- Now we actually have rename file support - maybe when you rename a
.jsfile to a.tsfile, we should offer to migrate the file.
Activity