-
Notifications
You must be signed in to change notification settings - Fork 187
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
Typescript #880
Typescript #880
Conversation
@mythmon I just realized that the approach I took of not including the extensions for TypeScript files will require users to use So as far as extensions, the options are
I'm leaning toward 1., what do you think? Update: I have a PR that will allow us to take Option 2 by fixing Vite so it's also happy with the .js extension for TypeScript files. |
Maybe I’m not understanding but it sounds like none of these options are backwards-compatible? So we’ll probably need to find another fourth option that is… |
Option 1 should be backward-compatible if we rewrote the imports to have the .js extension, right? This will avoid users having to specify |
do we want to commit yarn-error.log? |
Nope! Will remove 🙏 |
Duane found the prophesied fourth option: Use |
This is great! Thanks a lot @mythmon and @duaneatat I'll need advice on how to best update the other open PRs to avoid creating too much churn. A few other remarks/questions (of very low importance):
|
const c = curves.get(`${curve}`.toLowerCase() as CurveName); | ||
if (!c) throw new Error(`unknown curve: ${curve}`); |
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.
It works, but the logic escapes me a little: if curve is not a curveName, the curves.get(…) must still be allowed to proceed.
For example Plot.line(data, {curve: "BASIS"})
is correct, as is Plot.line(data, {curve: {toString: () => "BASIS"}})
.
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.
👍 makes sense!
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.
spoke to Fil about this, we can have the types be case-sensitive but still let the js be more permissive, so as it's written is good
Let me check on this
Yeah, tsc outputs 4 spaces and there isn't a config option to change it. If this is an issue for use we can format the output code in another step.
Updated so the scripts run in parallel 👍
This because we use ts-node to run the mocha tests (in order to allow writing tests in typescript). Even though it warns that this is experimental, it's safe to use it for our tests.
We can no longer point package.json's "exports" to src/index.js because |
Happy to work with you on that once this branch is merged (or stabilized, and we can rebase the outstanding branches onto this one) 👍 |
Thanks for all the explanations! 4 spaces is not an issue for me. If we now have all the compiled js files exported to dist/, I want to be clear about the implications. Does it mean that these files will become visible in npm/unpkg? Might we, then, have users who will want to rely on this (maybe because they'll want to import those files instead of the bundle)? Is this something we want to have—and want to support? |
In b87e8e3 I've added src/isoformat.d.ts as suggested by VSCode, but the unit tests (local and CI) fail on it. EDIT: solved in c9ed02b |
Another issue I have with these commits is that github doesn't see that the files have been renamed, adding even more churn. Should I do an explicit |
@@ -0,0 +1,11 @@ | |||
/* eslint-disable @typescript-eslint/no-explicit-any */ | |||
export function memoize1<T>(compute: (...rest: any[]) => T) { |
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.
Instead of using any
for the rest arguments, you can use unknown
. The difference is subtle, but TS will assume anything about an any
type, but it won't assume anything about unknown
.
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 sounds like very arcane knowledge, black belt! I'm still a yellow belt.
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.
Here's a source that talks a bit about the difference: https://www.typescript-training.com/course/fundamentals-v3/11-top-and-bottom-types/
Some useful quotes:
We can see here that any is not always a “bug” or a “problem” — it just indicates maximal flexibility and the absence of type checking validation.
any
is the way to tell Typescript that you don't know anything about the type, and that you want to back to the JS rules for types. It's useful, but anytime you do it you are weakening the type of the thing you're working with.
Values with an unknown type cannot be used without first applying a type guard
unknown
is the way to tell Typescript that you don't know anything about the type, and that you would like it's help to prove that what you are doing is type safe. It's useful because you are asking Typescript to help make the types stronger.
Git actually doesn't have any mechanism for storing file moves (unlike some other source control systems). Every move you see in a git tool is actually a file deletion and a file addition. Tools then use various heuristics to detect moves, like looking at the proportion of lines that have changed. For many of the files we've touched so far, we are changing the majority of the lines in the file, so the heuristic flags it as not a move. |
We should discuss the goal for this PR: When can we merge it? I'd propose that once we have confident that the build system can produce an artifact we could publish, and that this is a route we can use to add types, we should merge this. I don't think we should wait to convert the entire repository over to TS in this PR. We specifically designed this migration method to be parallelizable and incremental, so that we could avoid having one long lived branch that would stop all other work and/or produce difficult merge conflicts across the entire code base. To that point, I think we may be ready or close to ready to merge this. |
I still have this pending question: #880 (comment) |
Double-checked with Mike B about this -- src doesn't need to be a valid module, as long as it works in node when you yarn install it (which it does) then we're good to go |
Now that Plot 0.5.1 is released (and no hiccups), I'd like to rebase and merge this. |
…anging this… doesn't seem problematic
Rebased, seems to be working.
@mbostock ok to merge? |
package.json
Outdated
"test": "mkdir -p test/output && mocha -r module-alias/register 'test/**/*-test.js' test/plot.js && eslint src test", | ||
"prepublishOnly": "rm -rf dist && rollup -c", | ||
"test": "yarn test:typecheck && yarn test:lint && yarn test:mocha", | ||
"test:mocha": "mkdir -p test/output && mocha --conditions=mocha --files", |
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.
What does --conditions=mocha
and --files
do? I can’t find any documentation about these arguments.
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.
Okay, I was able to answer this myself. It’s a Node flag, not a Mocha flag, document here:
https://nodejs.org/api/packages.html#resolving-user-conditions
And it means that Node will respect the mocha
export in package.json here:
Line 16 in e1091a2
"mocha": "./src/index.js", |
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.
And as for --files
it doesn’t seem to have any effect so should probably be removed?
vite.config.js
Outdated
alias: [ | ||
{ find: "@observablehq/plot", replacement: path.resolve("./src/index.js") }, | ||
{ | ||
find: RegExp(`^(.*).js$`), replacement: "$1", customResolver: (importee, importer) => { |
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.
find: RegExp(`^(.*).js$`), replacement: "$1", customResolver: (importee, importer) => { | |
find: RegExp(`^(.*)\.js$`), replacement: "$1", customResolver: (importee, importer) => { |
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 pushed a few tweaks.
I’d appreciate it if someone could now take on the task of adopting Prettier so that formatting is enforced automatically.
I'll enable prettier in a follow-up PR 👍 I think I'll avoid doing a bulk formatting update though, and just have it triggered per-file as changes are made to avoid too many conflicts with other branches. Sound good? |
The approach taken here is to support converting 1 file at a time.