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

[react native] Missing export Ducoment for typescript #6198

Closed
maichongju opened this issue Oct 17, 2023 · 2 comments
Closed

[react native] Missing export Ducoment for typescript #6198

maichongju opened this issue Oct 17, 2023 · 2 comments

Comments

@maichongju
Copy link

maichongju commented Oct 17, 2023

How frequently does the bug occur?

Always

Description

I need to use the Document class because I am creating a customize get collection function

export function getCollection<T extends Document>(user: User, collectionName: string, databaseName = MongoDatabaseName) {
  return user.mongoClient(MongoServiceName).db(databaseName).collection<T>(collectionName);
}

This will raise an error {{TS2305: Module  'realm'  has no exported member  Document }}

Cause

The @realm did not export the Document type, but the realm package did. This causes the compiler to be unable to find the definition of Document type.

Workaround

I did find a workaround, by manually adding the realm package to the tsconfig.json, the compiler is able to find the type.

{"compilerOptions": {
  "paths": {
    "realm": ["./node_modules/realm/dist/bundle.d.ts"],
  }
}}

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

  1. install all dependency
{
  "name": "myawesomerealmapp",
  "version": "1.0.0",
  "scripts": {
    "start": "expo start --dev-client",
    "android": "expo run:android",
    "ios": "expo run:ios"
  },
  "dependencies": {
    "@realm/react": "^0.6.1",
    "expo": "^49.0.8",
    "expo-dev-client": "~2.4.8",
    "expo-splash-screen": "~0.20.5",
    "expo-status-bar": "~1.6.0",
    "react": "18.2.0",
    "react-native": "0.72.4",
    "react-native-get-random-values": "~1.9.0",
    "realm": "^12.2.1"
  },
  "devDependencies": {
    "@babel/core": "^7.22.5",
    "@babel/plugin-proposal-decorators": "^7.22.5",
    "@realm/babel-plugin": "^0.1.1",
    "@types/react": "~18.2.13",
    "typescript": "^5.1.3"
  },
  "license": "Apache-2.0",
  "private": true
}
  1. Create a file test.ts with the following code
import {Document} from 'realm'

Error will raise on the import line.

Version

"realm": "^12.2.1",, "@realm/react": "^0.6.0",

What services are you using?

Atlas App Services: Functions or GraphQL or DataAPI etc

Are you using encryption?

No

Platform OS and version(s)

Win11 23H2

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Oct 19, 2023

@maichongju

Your dependencies include TypeScript, and you might use:

import Realm from "realm";

type Document<IdType = any> = Realm.Services.MongoDB.Document<IdType>;
type MongoDBCollection<T extends Document> = Realm.Services.MongoDB.MongoDBCollection<T>;

Your Document can then be strongly typed.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Oct 19, 2023
@maichongju
Copy link
Author

@kneth

The workaround works! If this can be included in the documentation, then it might help other ppl who have the same issue.

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Oct 19, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants