-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
enable IDE auto-completion for the main classes #10618
base: develop
Are you sure you want to change the base?
Conversation
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.
some comments to explain why this works
@@ -25,7 +25,7 @@ import { utilKeybinding, utilRebind, utilStringQs, utilCleanOsmString } from '.. | |||
|
|||
export function coreContext() { | |||
const dispatch = d3_dispatch('enter', 'exit', 'change'); | |||
let context = utilRebind({}, dispatch, 'on'); | |||
const context = {}; |
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 works because JS autocompletion is powered by TypeScript, which is smart enough to infer the type of context
when defined as an empty object
utilRebind can safely be moved to the return
statement because this class does not use context.on
internally.
@@ -33,7 +37,7 @@ osmEntity.node = osmNode; | |||
|
|||
osmNode.prototype = Object.create(osmEntity.prototype); | |||
|
|||
Object.assign(osmNode.prototype, { | |||
const prototype = { |
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 required so we can use typeof prototype
in the JSDoc comment above. No runtime impact.
* @param {...Args} args | ||
* @returns {T & Pick<S, Args>} | ||
*/ | ||
export function utilRebind(target, source, ...args) { |
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 had to be refactored to use spread arguments instead of the arguments
keyword, because JSDoc doesn't support the arguments
keyword.
there's a new unit test to confirm that this
is still correctly bound.
Very nice! |
With only a few lines of changes + some JSDoc comments, we can significantly improve the DX in this codebase by enabling autocompletion/"intellisense" in most IDEs.
Currently, common classes like
context
andosmEntity
are typed asany
, so your IDE can't help you. With this PR, we get autocomplete for most of the class methods:We can also reference common classes from JSDoc comments, such as
iD.Context
oriD.Graph
: