-
Notifications
You must be signed in to change notification settings - Fork 27
JS type and name resolution #36
base: master
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,242 @@ | |||
In Javascript files, the compiler understands Typescript types in | |||
JSDoc comments like `@type` and `@param`. However, it has additional |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a link to the page you pointed me to? (With a TODO to switch to the new page when it's in this repo?)
`Number` | `number` | ||
`String` | `string` | ||
`Boolean` | `boolean` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good to know, but very confusing... I'm guessing that in jsdoc either they're the same or people don't make a distinction, but then I saw that TS does have Number
etc, and I can't figure out the difference or why number
is preferable. (Maybe this is the answer, and if so add a link to it, or better, to the thing it points to.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather link to another reference page that explains the difference. I didn't use the correct terms here, which probably made googling harder.
reference/JSDoc-Type-References.md
Outdated
The rewrite `Object -> any` is disabled when `"noImplicitAny": true`, | ||
which the compiler takes as a signal that the code is being written | ||
with Typescript in mind. Old versions of Typescript also rewrote | ||
`object -> any`, but stopped in Typescript 3.7. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Even more confusing for Object
vs object
...)
`Null` | `null` | ||
`Undefined` | `undefined` | ||
`Void` | `void` | ||
`function` | `Function` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yet more confusion, especially since this is unlikely to have the same explanation as the above--?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will write more explanations for all of these.
reference/JSDoc-Type-References.md
Outdated
`Object<number,Type>` | `{ [x: number]: Type }` | ||
|
||
Finally, `Array` and `Promise` both default their type parameters to | ||
`any` when no type parameters are provided: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side-question: shouldn't these be using unknown
when noImplicitAny
is on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not how it works in JS -- JS defaults to any -- although perhaps it should. You are probably thinking of inference's default, but elsewhere in TS you just get an error, and the resulting variable has type any
:
var a: Array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was thinking that would happen only if you're using noImplicitAny
. (And an unknown
seems to me a bit better than an error, since it would lead to other errors which will clarify that you need to say what's in it.) In any case, it's a tangent...
frabjous: true, | ||
beamish: true, | ||
callooh: "callay", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Made me spill some time on google...)
reference/JSDoc-Type-References.md
Outdated
Expando declarations are those that start with an initial declaration | ||
and add properties to it, like so: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Probably not the right place, but I get the feeling that these expando things deserve their own page?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm probably so. Let me write a bit about aliasing here and then I'll think about whether to move it into a new page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I started on aliasing and I think Assignment Declarations need their own page, which will cover expandos and various kinds of aliasing.
I'll remove it from this PR.
reference/JSDoc-Type-References.md
Outdated
Like expando, except that you are often directly making entries in the | ||
module's symbol table instead. | ||
|
||
TODO: Explain aliasing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether this TODO is repeated or maybe it is about some other kind of aliasing (since I don't know how commonjs is related here, isn't it just module-level thing?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler tries even harder to figure out patterns of aliasing for module.exports
, which means that this section will expand on the previous one.
reference/JSDoc-Type-References.md
Outdated
<!-- | ||
## Weird Stuff ## | ||
|
||
- `@enum` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I've seen (after your "oh boy, @enum" comment, which made me go look), this would probably be a useful addition...
|
||
`getIntendedJSDocTypeReference` in src/compiler/checker.ts. | ||
|
||
## Values as Types ## |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be honest, I'd prefer to never mention this in any documentation ever so that we don't imply that we think relying on it is a good idea (it's not).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like maybe we could talk about it in the TypeScript necronomicon or something, but I'd leave it out of the handbook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reference pages are the necronomicon, or the closest we'll get in an open-source team.
This is why I've never written it down before, though. I hoped it would "just work" for most people.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But, like, rust
has a hard split between "The Rust Book" and "The Rustonomicon" (which is what I was referencing before). While documenting the current state of esoteric edge cases in, say, conditional type resolution or js type lookup is laudable, I don't think it should be adjacent to information that's much more mainstream - it serves mostly to distract. Like, I think the "what simple remappings do we do (String -> string
)" is more useful for casual reading and documentation, while "what fallback code exists within the compiler, approximately" is heavyweight "I wanna know how the compiler internals tick" stuff. This article does a bit of both - I'd split it into two pieces, down those viewership lines, with one targeted at each audience, tbh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're going for the same two categories, but with a softer split. Look at the other documents in reference/ -- they cover the same kinds of things.
I think it is worthwhile to put the first section's information into the handbook under checkJs though.
It's just that that chapter doesn't exist yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that mean that Compiler Options
belongs elsewhere? Because that is just a reference, while the rest are really technical internal discussions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably. Not sure where it should go, though. @RyanCavanaugh opinions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think this is good case for splitting into two articles - my goal when I get my hands on doing new handbook stuff is to try split the existing doc into three main camps:
- Language Learners
- Experts looking to debug why something isn't what they expect
- People wanting to faff with tooling
And each doc on any topic should ideally only be trying to address one of those three people (e.g. don't try mix the descriptions of config settings deeply in with a language feature)
I expected we'd need a bunch of docs for JSDoc though, so I wouldn't worry too much now about how it splits because once it starts becoming public facing we'll have more info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@orta when you say "faff with tooling", are you thinking of API consumers? This article is mostly people interested in compiler internals, with a touch of "experts' debugging help". That's what I was thinking of for the Assignability page too; realistically knowing the mechanics of assignability isn't going to help debugging much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example "TypeScript with webpack" or "TypeScript with eslint" user-level stuff - not this depth 👍
Re-use FOO | BAR example throughout the algorithm explanation.
JSDoc authors wanted to use `object` with its Typescript meaning. The | ||
rewrite `Object -> any` is disabled when `"noImplicitAny": true`, | ||
which the compiler takes as a signal that the code is being written | ||
with Typescript in mind. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe drop "which ... mind" since it's mentioned at the top?
This document came out of conversations with Eli, who pointed out that we don't have a good description of how types are resolved. The current handbook just hopes that type references will "just work", so omits any discussion.
Once I got started writing a reference page, the level of detail is right for explaining name resolution JS as well. I didn't finish this, but I wanted to send the type resolution explanation out for review first. I can cut the name resolution sections for later if needed.