Skip to content

Make the new JS API a bit more idiomatic#3200

Merged
nex3 merged 4 commits intomainfrom
js-api-improvements
Nov 30, 2021
Merged

Make the new JS API a bit more idiomatic#3200
nex3 merged 4 commits intomainfrom
js-api-improvements

Conversation

@nex3
Copy link
Contributor

@nex3 nex3 commented Nov 11, 2021

This takes more advantage of overloads rather than using factory
constructors or methods with different names.

@nex3 nex3 requested a review from Awjin November 11, 2021 22:32
@nex3 nex3 marked this pull request as draft November 11, 2021 22:33
@nex3
Copy link
Contributor Author

nex3 commented Nov 11, 2021

I'm basing this on the JS API doc branch so that you can review with documentation, but if it looks good I'll rebase onto main and update the tests and the embedded host.

@nex3 nex3 force-pushed the js-api-improvements branch from 67455d2 to 2d925ae Compare November 12, 2021 21:32
@nex3 nex3 changed the base branch from new-api-docs to main November 12, 2021 21:37
@nex3 nex3 marked this pull request as ready for review November 12, 2021 21:37
@nex3 nex3 force-pushed the js-api-improvements branch from 2d925ae to e7a659a Compare November 12, 2021 21:38
nex3 added 2 commits November 12, 2021 13:41
This takes more advantage of overloads rather than using factory
constructors or methods with different names.
@nex3 nex3 force-pushed the js-api-improvements branch from e7a659a to a50c1bb Compare November 12, 2021 21:42
lightness: number,
alpha?: number
): SassColor;
constructor(options: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the 3 overloads all have a single argument and the same return type (none as this is a constructor), the idiomatic way to write such typescript types is to use a union type instead of overloads: https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#use-union-types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed elsewhere, constructor overloads are necessary here in order to disallow new Color({hue: ..., red: ...}).

get alpha(): number;

changeRgb(options: {
change(options: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idiomatic way to write this is also to use a union rather than overloads (btw, that's how you did it in embedded-host-node)

@nex3 nex3 merged commit ebbc528 into main Nov 30, 2021
@nex3 nex3 deleted the js-api-improvements branch November 30, 2021 00:10
mirisuzanne pushed a commit that referenced this pull request Feb 10, 2022
This takes more advantage of overloads and unions rather than
using factory constructors or methods with different names.
mirisuzanne added a commit that referenced this pull request Feb 10, 2022
* main: (149 commits)
  Add the sourceMapIncludeSources option in the new JS API (#3226)
  Fix the toc tool to insert the toc (#3225)
  Fix some incorrect documentation/typings in LegacyPluginThis.options (#3246)
  Add a type annotation for the top-level NULL field (#3243)
  Add type annotations for the top-level TRUE and FALSE fields (#3241)
  Add type annotations for the sass.types.Error class (#3238)
  Add a type declaration for LegacyPluginThis.options.context (#3236)
  Make LegacyAsyncFunction type more usable (#3237)
  Allow SassFunction signatures to be checked on return (#3220)
  Make LegacyFileOptions.data optional (#3215)
  Use the URL type from the DOM definitions, not from Node (#3214)
  Document the new JS API (#3183)
  Make the new JS API a bit more idiomatic (#3200)
  Update immutable-js dep. (#3195)
  Add a code example for Logger.silent (#3192)
  Update dependencies (#3193)
  Replace FileImporterResult with a plain URL (#3180)
  Convert explicit compatiblity HTML into a nicer @-tag
  Convert explicit "Heads up" HTML into nicer-looking Markdown
  Automatically handle absolute file: URLs for FileImporters (#3181)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants