Skip to content
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

Fix exports #69

Closed
parzhitsky opened this issue May 31, 2020 · 1 comment · Fixed by #72
Closed

Fix exports #69

parzhitsky opened this issue May 31, 2020 · 1 comment · Fixed by #72
Labels
Change: minor [Issue / PR] describes a non-breaking change, such as adding a new functionality Domain: main [Issue / PR] describes change in the functionality, its optimization Priority: low [Issue / PR] could be addressed at any convenient time Type: improvement [Issue / PR] addresses lack of a functionality or an open possibility of enhancement
Projects

Comments

@parzhitsky
Copy link
Member

Currently, all packages (xrange, @xrange/core, @xrange/func) support both default and assigned imports, which is good and all when it comes to exporting only xrange function itself:

import xrange from "{PACKAGE}"; // 🚧 only with `esModuleInterop: true`
import xrange from "{PACKAGE}/dist"; // 🚧 only with `esModuleInterop: true`
import xrange from "{PACKAGE}/dist/index"; // 🚧 only with `esModuleInterop: true`
import xrange from "{PACKAGE}/dist/xrange"; // 👍
// javascript
const xrange = require("{PACKAGE}"); // 👍
const xrange = require("{PACKAGE}/dist"); // 👍
const xrange = require("{PACKAGE}/dist/index"); // 👍
const xrange = require("{PACKAGE}/dist/xrange"); // 🚧 access through `xrange.default()`

But for type definitions the support is less convenient, especially when esModuleInterop is false:

import type { Predicate } from "@xrange/func"; // 🚧 only with `esModuleInterop: true`
import type { Predicate } from "@xrange/func/dist"; // 🚧 only with `esModuleInterop: true`
import type { Predicate } from "@xrange/func/dist/index"; // 🚧 only with `esModuleInterop: true`
import type { Predicate } from "@xrange/func/dist/xrange"; // 👍

// ❌ invalid syntax
import type { Predicate } = require("@xrange/func"); // ❌
import type { Predicate } = require("@xrange/func/dist"); // ❌
import type { Predicate } = require("@xrange/func/dist/index"); // ❌
import type { Predicate } = require("@xrange/func/dist/xrange"); // ❌

// ❌ invalid syntax
import { Predicate } = require("@xrange/func"); // ❌
import { Predicate } = require("@xrange/func/dist"); // ❌
import { Predicate } = require("@xrange/func/dist/index"); // ❌
import { Predicate } = require("@xrange/func/dist/xrange"); // ❌

Exporting types from separate files wouldn't help much:

// ❌ invalid syntax
import type Predicate = require("@xrange/func/dist/typings"); // ❌
import type Predicate = require("@xrange/func/dist/typings/predicate"); // ❌

// 🚧 not a type import
import Predicate = require("@xrange/func/dist/typings"); // 👍
import Predicate = require("@xrange/func/dist/typings/predicate"); // 🚧 verbose path
@parzhitsky parzhitsky added Change: major [Issue / PR] describes a breaking change of any kind Domain: main [Issue / PR] describes change in the functionality, its optimization Pending: unclear [Issue] not yet fully defined Priority: low [Issue / PR] could be addressed at any convenient time Type: improvement [Issue / PR] addresses lack of a functionality or an open possibility of enhancement labels May 31, 2020
@parzhitsky parzhitsky added this to To be considered in xrange@3.0 May 31, 2020
@parzhitsky parzhitsky changed the title Swap export files Fix exports May 31, 2020
@parzhitsky
Copy link
Member Author

parzhitsky commented May 31, 2020

Suggestion: microsoft/TypeScript#38866

Weird but working solution would be to make an assigned export of xrange, as well as named (not default!) export of each of the types, — all from src/index.ts; this would require using @ts-ignore instruction:

src/index.ts:

import xrange from "./xrange";

// @ts-ignore
export = xrange;

export { XRange } from "./typings/xrange";
export { Predicate, NextFactory } from "@xrange/core";

Make sure to re-export only type definitions: this will not be checked by TypeScript compiler


some/third-party/module.ts

import xrange = require("xrange"); // 👍
import xrange from "xrange"; // 🚧 only with `esModuleInterop: true`
import xrange, { Predicate, NextFactory } from "xrange"; // 🚧 only with `esModuleInterop: true`
import type { Predicate, NextFactory } from "xrange"; // 🚧 only with `esModuleInterop: true`

some/third-party/module.js

const xrange = require("xrange"); // 👍
const xrange = require("xrange").default; // ❌

@parzhitsky parzhitsky removed the Pending: unclear [Issue] not yet fully defined label May 31, 2020
@parzhitsky parzhitsky linked a pull request Jun 3, 2020 that will close this issue
@parzhitsky parzhitsky added this to To be considered in xrange@2.1 via automation Jun 3, 2020
@parzhitsky parzhitsky moved this from To be considered to Merged / Done in xrange@2.1 Jun 3, 2020
@parzhitsky parzhitsky moved this from To be considered to Rejected in xrange@3.0 Jun 3, 2020
@parzhitsky parzhitsky added Change: minor [Issue / PR] describes a non-breaking change, such as adding a new functionality and removed Change: major [Issue / PR] describes a breaking change of any kind labels Jun 3, 2020
xrange@3.0 automation moved this from Rejected to Merged / Done Jun 3, 2020
@parzhitsky parzhitsky moved this from Merged / Done to Rejected in xrange@3.0 Jun 4, 2020
@parzhitsky parzhitsky removed this from Rejected in xrange@3.0 Jun 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Change: minor [Issue / PR] describes a non-breaking change, such as adding a new functionality Domain: main [Issue / PR] describes change in the functionality, its optimization Priority: low [Issue / PR] could be addressed at any convenient time Type: improvement [Issue / PR] addresses lack of a functionality or an open possibility of enhancement
Projects
No open projects
xrange@2.1
  
Merged / Done
Development

Successfully merging a pull request may close this issue.

1 participant