TypeScript, MongoDB and Reference Issues - node.js

I've got a NodeJS app (built in Visual Studio) which uses TypeScript, and MongoDB.
I've written a small library which wraps the MongoDB driver in some TypeScript classes and just today it started acting up.
If I include this line at the top of one of my library files:
/// <reference path='./_scripts/typings/mongodb/mongodb.d.ts' />
it build briefly, before begining to complain about "Duplicate identifier"s, and lots of them, for example:
Error 6 Type name 'Transform' in extends clause does not reference constructor function for '"stream".Transform'.
Error 7 Type name 'events.EventEmitter' in extends clause does not reference constructor function for 'events.EventEmitter'.
Error 8 Duplicate identifier 'errno'.
Error 9 Duplicate identifier 'code'.
Error 10 Duplicate identifier 'path'.
So I take that out, and everything is briefly ok, until I start seeing issues reported around this line:
import mongodbNS = require('mongodb');
If I were to then remove that line, I would see errors with things like:
public Connect: () => Promise<mongodbNS.Db>;
I can muddle through by adding a removing things and taking advantage of the brief window of compilability which opens up - but clearly this is not the way.
What is?

and they all come from node.d.ts
This is happening because you have two versions of node.d.ts (i.e. different files on disk) in your project.

Related

How can I declare global Error type in typescript NodeJs Architect project?

Context
Hi, I have a NodeJs+Ts+Architect setup for building and deploying lambda functions. Architect uses typescript plugin to compile typescript. I am trying to use Error class to throw errors.
However, Typescript is picking up Error type from
/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/lib.es5.d.ts.
In the image below, please note the constructor signature only accepts message field. And the error interface does not have an options object either. Please
look at Browser Error Class or NodeJs Error Class to see the signatures.
Node Error has the following constructor signature and Error interface.
Problem
Getting TS Error for trying to use constructor signature of Node Error as Typescript is reading Error type from lib.es5.d.ts which accepts only 1 argument
Possible Solutions which I know
Declare global Error type ( Need help here. Since Architect is compiling TS using its plugin, I am not able to declare and override Error interface )
Use your own Error class
I hope the question made sense. Would appreciate if there is a nicer way to solve this, but I am not getting ample discussions on Architect+Ts+NodeJs.
The cause option was introduced in ES2022. You would need to use at least that version for your lib types.
Change the lib version in your tsconfig.json to:
"compilerOptions": { "lib": ["es2022"] }

How can I fix the "Error: 'default' is not exported by..." when using formdata-node default import?

Summary
I feel like I've hit my head against a wall too many times trying to figure out where I may be going wrong. I have a simple piece of typescript code:
import FormDataI from 'formdata-node'
const c = new FormDataI()
export { c }
That im trying to compile down to umd using rollup and typescript. For some reason, I cant seem to get it to compile correctly and instead receive an error message:
[!] Error: 'default' is not exported by node_modules/formdata-node/lib/FormData.js, imported by index.ts
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
index.ts (1:7)
Any suggestions as to where I may be going wrong? Here is a sample project that throws the error.
Things I've tried
Playing with various typescript settings such as esModuleInterlop
Playing with the commonjs properties such as requireReturnsDefault
Stepping through the rollup code hoping to find the import error. It appears to not notice the export from formdata-node which is exported using module.exports.default and module.exports
UPDATE
In the case of the above situation, I could simply set it as a global/external since FormData is supported in the browser natively. The question is, why does this error appear at all though?

Data with SwiftUI and CoreData

I'm trying to display data in SwiftUI from a .xcdatamodeld file
If I make the class I've got an error "Invalid redeclaration of ..."
And if I delete the class and I use only the extension I've got the error "Use of undeclared type ...
(Links lead to images)
What can I do ?
Thank you
Assuming ListeVetements is an entity defined in your model and you have left the code generation option in your model for this entity to Class:
Your second approach (define a static func in an extension) should actually work.
But as you have played around with creating the classes yourself, try clean cmd+shift+k.
These code generation errors sometimes be very sticky. closing Xcode and deleting derived data sometimes helps rm -rf ~/Library/Developer/Xcode/DerivedData

Duplicate identifier 'nodeName' in jquery.d.ts

I have referenced jquery.d.ts in my TypeScript Project in Visual Studio 2012 and am getting the following error:
Error 1 Duplicate identifier
'nodeName' C:\DEV\MyProject\Scripts\JQuery\jquery.d.ts 786 2 pageMain.ts
Which leads to the following code in jquery.d.ts:
interface EventTarget {
nodeName: string;
}
I have restarted VS2012, and then again after deleting all the .sou files for the project.
I have checked all my references (includes) and found no other reference to EventTarget.
I have looked in lib.d.ts, and indeed, there is a EventTarget interface, but it does not implement nodeName.
FYI: I found two copies of lib.d.ts on my machine, and checked them both: does anyone know which one is used by the compiler? I did mods on both (changing the window.alert to window.alert2), but they had no effect.
Your jQuery.d.ts needs to be updated.
Here is a quick way to test the issue:
declare var x: EventTarget;
x.nodeName = '';
If you run this on the TypeScript Playground, you'll see that nodeName does not exist on EventTarget. Indeed, if you look at the lib.d.ts file, you'll find this:
interface EventTarget {
removeEventListener(type: string, listener: EventListener, useCapture?: bool): void;
addEventListener(type: string, listener: EventListener, useCapture?: bool): void;
dispatchEvent(evt: Event): bool;
}
However, if you run the same code within Visual Studio with Web Essentials, you'll find that nodeName does exist on EventTarget. This is because Web Essentials is using a newer version of lib.d.ts - if you disable Web Essentials, you would get the same error as you see in the Playground.
These issues will happen occasionally as the standard definitions are improved to keep up with what browsers actually support. Your definition files should be adjusted to keep in line with your development tools, with a preference to changing files such as jquery.d.ts rather than lib.d.ts.
Interesting bug, I faced same issue with className instead of nodeName
After spending sometime trying to solve the issue I found this article link in jquery.d.ts file
I am maintaining general.d.ts file to create required interface items needed.
I added the following definition to the file
interface EventTarget {
className: string;
}
Immediately I got an error message of duplicate identifier.
I kept it and saved general.d.ts, and went back to my original working file, I found the compiler passed through.
My assumption it is Web Essentials bug failing to load definitions properly.

WCF DataService EF entities not found

I have an EDMX model with a generated context.
Now i generated a Self Tracking Entities library is separate project and referenced this from the EDMX model.
Also set the correct namespace in the context to the same namespace as the entities.
Now working with this all works except when i try to create a WCF data service with this context.
So just create new ObjectContext and working with it directly works fine.
But having referenced the context + model lib and the entities lib i get the following error when loading the service
The server encountered an error processing the request. The exception message is 'Value cannot be null. Parameter name: key'. See server logs for more details. The exception stack trace is:
Now i found that this could happen when using data service with external entity lib and fix was overriding the createcontext
with code
Collapse
System.Data.Metadata.Edm.ItemCollection itemCollection;
if (!context.MetadataWorkspace.TryGetItemCollection
(System.Data.Metadata.Edm.DataSpace.CSSpace, out itemCollection))
{
var tracestring = context.CreateQuery<ClientDataStoreContainer>("ClientDataStoreContainer.DataSet").ToTraceString();
}
return context;
Now the error is gone but i get the next one and that is:
Object mapping could not be found for Type with identity 'ClientDataStoreEntities.Data'.
This error occurs on the .toTraceString in the createcontext
the ssdl file has the defined type
Collapse
<EntitySetMapping Name="DataSet">
<EntityTypeMapping TypeName="IsTypeOf(ClientDataStoreEntities.Data)">
So it has to load the ClientDataStoreEntities.Data type which is the namespace and type of the STE library that i have generated from the model.
EDIT: with
var tracestring = context.CreateQuery<Data>("ClientDataStoreContainer.DataSet").ToTraceString();
It does seem to load all types , however now the service does not have any methods that i can call.
there should be 2 DataSet and PublishedDataSet but:
<service xml:base="http://localhost:1377/WcfDataService1.svc/">
−
<workspace>
<atom:title>Default</atom:title>
</workspace>
</service>
is what i get.
I ran into the same issue (the first one you mention). I have worked around using the suggestion by Julie Lerman in this thread. The other suggestion didn't work for me although I will experiment with them more since Julie's solution may have performance implications since it's executed (and has some cost) for every query.
MSDN Fail to work with POCO ModelContainer which entities are located in other assembly
Edit: Sorry, just realized you utilized the other solution mentioned in this thread.

Resources