microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.7k stars 12.45k forks source link

`esModuleInterop: true` with class static method #45133

Open bluelovers opened 3 years ago

bluelovers commented 3 years ago

Bug Report

🔎 Search Terms

class static import

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?module=1#code/JYWwDg9gTgLgBAYQJIBEA0cDedgGdkpwC+cAZlBCHAOQDGwAJrtQNwCwAUJwKYAeksMgFcAdrRjAIIuGACGMABYAVCAkYAKegwBccXDCjARAczgAfRKgCUu-YZOdMnAJDBScdQQB0eApsZWVi5OHM7OUNwwQlDSWl4wEADKBkbG6kGhRJwuEVExcHERYAA2srTc6gD0AHoAOpXAYADaIqQAurj1lRjU1BlZXBx8AvCkouKS0nKKKmoMAEz+OnopJuaWKDYr9saOLm4evqhLgcEu4ZHRsYzxSatpGc4DOZf5hdwlZRU19Y0t7Z1Kt0aH1OM8ONNlKoNL0MpDZoxFrDOEA

💻 Code

import CID, { isCID } from 'cids';

export function pathToCid(cid: string | CID): string
{
    if (CID.isCID(cid))
    {
        return cid.toString()
    }

    return cid.replace(/^\/ip[nf]s\//, '')
}

export function pathToCid2(cid: string | CID): string
{
    if (isCID(cid))
    {
        return cid.toString()
    }

    return cid.replace(/^\/ip[nf]s\//, '')
}

pathToCid('')
pathToCid2('')

🙁 Actual behavior

TypeError: (0 , cids_1.isCID) is not a function

🙂 Expected behavior

rbuckton commented 12 months ago

Normally I would say this should be considered an error as a native ES module wouldn't normally provide the entire module as the default export. However, that's exactly what NodeJS does for its built-ins like fs when you use import in a native module.

It is odd that the following works:

import CID from 'cids'; // passes through __importDefault
import { isCID } from 'cids'; // directly accesses module object

while the combined version doesn't:

import CID, { isCID } from 'cids'; // passes through __importStar, doesn't adjust for default import

@andrewbranch, @weswigham: Any thoughts on this? I'm leaning towards trying to make it work under --esModuleInterop rather than reporting an error, given that the individual declarations work in this mode.