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

Typescript import issue in 6.4.x #197

Closed
conroysj opened this issue Mar 10, 2020 · 12 comments
Closed

Typescript import issue in 6.4.x #197

conroysj opened this issue Mar 10, 2020 · 12 comments
Labels

Comments

@conroysj
Copy link

I'm getting the following error when I try to run my app after upversioning to 6.4.x

6.3.0 works fine.

TypeError: (0 , _spacetime2.default) is not a function
formatDate
src/utils/date.js:95
  92 |       dateFormat = format;
  93 |   }
  94 | 
> 95 |   return spacetime(dateValue, TIMEZONE).unixFmt(dateFormat);
  96 | }
  97 | 
  98 | export function getNextStatementDate() {

have tried importing like
import spacetime from 'spacetime';
and
import { spacetime } from 'spacetime';

have tried a clean wipe of my repo, fresh install with 6.4.x to no avail. Logging spacetime() gives me /static/media/spacetime.edf1835b.mjs which makes me think it might be a webpack compatibility issue. Running webpack@3.8.1

@spencermountain
Copy link
Owner

thanks Sean, just double-checking, this is a typescript error right?
I was able to reproduce this just now, but only using ts-node
cheers

@conroysj
Copy link
Author

ts-node is used by my nodemon and as a devDependency for several of my dependencies

@conroysj
Copy link
Author

I don't see evidence of it as being the root cause, however.

@spencermountain
Copy link
Owner

hey Sean, I took a look at this, and haven't seen any major changes between versions. Did anything else change on your end? I'd expect others to have the same issue, if the module was borked.

You may want to change your tsconfig.json by adding "esModuleInterop": true. Make sure to run tsc --init when starting a new Typescript project.
let me know
cheers

@es-lee
Copy link

es-lee commented Mar 27, 2020

I have same issue. When I build it with tsc, I got the following error

src/test.ts:1:8 - error TS1192: Module '"spacetime"' has no default export.

1 import spacetime from 'spacetime'

test.ts is just same as readme typescript example

It seems like importing only types not real modules

@spencermountain
Copy link
Owner

thank you @es-lee will look at this today.

@spencermountain
Copy link
Owner

hey, i should admit, I am not a typescript pro like @jecraig is.
but I could reproduce the issue moving from 6.3 to 6.4 despite it seeming like a really harmless change

i could get it working like this, but the typefile no-longer works.

import * as spacetime from './'
spacetime().format()

gonna dig a little more, but I should admit i'm a little out of my depth here.
Worse case, we just update the typescript in the readme.
Thanks for your patience

@spencermountain
Copy link
Owner

ah, geez
yeah, you can also avoid this error by adding this to tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

i'm not sure why this has changed during an update yet. We have an esmodule build which should export the right thing, and not require the interop flag. But I can't get typescript to point at that build for some reason.
any help here is very appreciated.

@jecraig
Copy link
Contributor

jecraig commented Mar 27, 2020

@spencermountain I haven't been able to get a typescript environment that reproduces the issue. If you could let me know what environment you had (or a failing repo) I could possibly look into it more.

Regardless, I read through the PR you linked to see if I could find anything. Now I generally regard package making to be complete blackmagic so I might not be the best person to debug it but I noticed you added "sideEffects": false to package.json. I wonder if the export is getting messed up during tree shaking because of this.

@spencermountain spencermountain changed the title 6.4.x not being imported correctly Typescript import issue in 6.4.x Apr 4, 2020
@spencermountain
Copy link
Owner

spencermountain commented Apr 4, 2020

thanks Jacob, this was a clever insight. Thank you.
Yeah, I think this "esModuleInterop": true is necessary, for loading most npm libraries?
Typescript doesn't like module.export and doesn't like export default and I sometimes feel like typescript is a bitter joke on maintainers.
Will keep this open, if anyone wants to shed some light on this. But for now, we have the esModuleInterop advice on the readme.

for reference, i'm just trying things, running npm run build then hit a tmp.ts file ts-node ./tmp.ts:

import spacetime from './'
let d = spacetime('june 5')
console.log(d.format('nice'))

@Nfinished
Copy link
Contributor

The error I'm seeing is

Subsequent property declarations must have the same type.  Property 'every' must be of type '(unit: TimeUnit, end: Spacetime) => Spacetime[]', but here has type '(unit: TimeUnit, end: String) => Spacetime[]'.

61   every: (unit: TimeUnit, end: String) => Spacetime[]
     ~~~~~

  node_modules/spacetime/types/types.d.ts:60:3
    60   every: (unit: TimeUnit, end: Spacetime) => Spacetime[]
         ~~~~~
    'every' was also declared here.

Should

every: (unit: TimeUnit, end: Spacetime) => Spacetime[]
every: (unit: TimeUnit, end: String) => Spacetime[]

just be expressed as every: (unit: TimeUnit, end: String | Spacetime) => Spacetime[]?

From https://github.com/spencermountain/spacetime/blob/master/types/types.d.ts#L59-L61

This is potentially a separate issue.

@jecraig
Copy link
Contributor

jecraig commented Jun 16, 2020

@Nfinished I believe this would be a separate issue. I'll have a PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants