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 Not Working When Package.json type is "module" #549

Open
Lozoute opened this issue Mar 11, 2024 · 5 comments
Open

TypeScript Not Working When Package.json type is "module" #549

Lozoute opened this issue Mar 11, 2024 · 5 comments
Assignees
Labels

Comments

@Lozoute
Copy link

Lozoute commented Mar 11, 2024

Describe the bug
When installing Nylas SDK 7 to a new repo, using TypeScript 5 and type "module" in package.json, TypeScript throws the error This expression is not constructable. adding type: "module" into the package.json of Nylas fixes the problem but then commonjs requires do not work anymore.

To Reproduce
1 Create a new repo

2 Add this package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "tsc && node nylas.mjs"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nylas": "^7.2.1"
  },
  "devDependencies": {
    "typescript": "^5.4.2"
  }
}

3. Add this tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "declaration": true,
    "esModuleInterop": false,
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "strictFunctionTypes": false,
    "strictBindCallApply": false,
    "strictPropertyInitialization": false,
    "skipLibCheck": true,
    "resolvePackageJsonExports": true,
  },
  "exclude": ["modules/clients/**/*"]
}

4. Add this nylas.mts file

import Nylas from 'nylas';


const apiKey = 'YOUR_API_KEY';
const apiUri = 'https://api.eu.nylas.com';

const nylas = new Nylas({
  apiKey: apiKey,
  apiUri: apiUri
});


const result = await nylas.messages.list({
  identifier: "YOUR_GRANT",
  queryParams: {
    limit: 20,
  },
});

console.log(result);

5. npm install && npm start

Expected behavior
Well I guess we should have no error but the tsc compiler throws the error This expression is not constructable.

SDK Version:
7.2.1

Additional context
Add any other context about the problem here.

@Lozoute Lozoute added the bug label Mar 11, 2024
@mrashed-dev mrashed-dev self-assigned this Mar 12, 2024
@mrashed-dev
Copy link
Contributor

Thanks for opening this issue @Lozoute, we'll take a look and provide you an update as soon as we get one.

@mrashed-dev
Copy link
Contributor

Hey @Lozoute, we're still looking into this internally and trying to find the best course of action here that won't break anything for cjs users. Until then, during my testing I found that if you use new Nylas.default({... it should work and it does give you full typing support. I'm not suggesting this as a final solution, but only a temporary workaround until we have something flushed out that can satisfy everyone. Hybrid projects in TS are always a little tricky. Sorry for the delay on this, but we're looking to resolve this fully soon.

@Lozoute
Copy link
Author

Lozoute commented Jul 30, 2024

Hello @mrashed-dev
It may work for TypeScript but once compiled, this code won't work:

{
  "errorType": "TypeError",
  "errorMessage": "Nylas.default is not a constructor",
  "trace": [
    "TypeError: Nylas.default is not a constructor",
    "    at file:///var/task/src/index.mjs:9:15",
    "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
  ]
}

I think it's crazy that the first thing we have on the NodeJs SDK Documentation is this:

import Nylas from "nylas";

const nylas = new Nylas({
  apiKey: "<NYLAS_API_KEY>",
  apiUri: "<NYLAS_API_URI>"
})  

And it doesn't even work properly.

@Lozoute
Copy link
Author

Lozoute commented Jul 30, 2024

My only working solution for now is to do this:

// @ts-expect-error Some comment so next dev can understand...
const nylas = new Nylas({
  apiKey: nylasApiKey,
  apiUri: nylasApiBaseUrl
}) as Nylas.default

@a-gasior
Copy link

@mrashed-dev this is still not resolved. The hack suggested by @Lozoute still works.

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

3 participants