How to get the type/interface of an unknown imported function in TypeScript - node.js

I've heard that it is bad practice to set an expected type of a variable to any.
However, I don't understand, how I can get a return type of an imported function, which returns an object, which is only used in the library.
As an example, if I would like to use the crypto.createCipheriv() function provided by Node.js, which returns a Cipher object as stated in the docs, I would not known how to give a variable this type.

const variable: ReturnType<crypto.createCipheriv>;

Related

how to check if a variable is Firestore Document Data type?

I am using Typescript and NodeJS for my Google Cloud Function
if I get a document from firestore like this
const result = await db.doc(`events/${eventID}`).get();
const myData = result.data();
then the type of myData is FirebaseFirestore.DocumentData like this
in other part of my code,
I need to check, if a variable has the type of FirebaseFirestore.DocumentData or not using the code below
if ( myVariable instanceof FirebaseFirestore.DocumentData ) {
// do something
}
but I have an error like this
Property 'DocumentData' does not exist on type 'typeof
FirebaseFirestore'
so how to check if my variable is a type of Firestore Document Data?
I see this answer , and it needs to import something so I can access it. how to do something like that for FirebaseFirestore.DocumentData ?
As with your other question you are mixing up Typescript types and JavaScript objects.
FirebaseFirestore.DocumentData refers to a type that is used by the TypeScript compiler as a compile time check. TypeScript compiles down to JavaScript and JavaScript doesn't know about those types during runtime. So the only way to check if the returned data is the Document Data you have to check for the existence of fields or check for undefined.

How to read visual studio code intellisense syntax hint, any document for operators?

VSC like VS gives out syntax/signature hint. I understand : means data type
myText: string // : means datatype of myText is string
myStuff: any // any means can be any data type.
sometimes hard to guess what the operators mean, for example the Node's request(),
my understanding is
const request means I can define any variable like const x=request(...) or var x=request(...).
request.RequestAPI means it's an API call.
options: defines this parameter is a typical object-like options in form of {...}
(request.UriOptions & request.CoreOptions) I understand the beginning and end parts, they must be enum of Uri and Core, but what is &? Does it mean I need to supply both Uri AND Core?
| does this pipe mean OR? If it is then it's duplicating the part before the pipe.
callback?: request.RequestCallback, so here I must provide a callback which will be typed (or functioning) as RequestCallback, but what is ?:?
Is there any document for these conventions?
I wanted to comment, because I don't know the complete answer, but here is some helpful information:
You are probably seeing this definition of DefinitelyTyped: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/request/index.d.ts#L29
Have a look at this to understand the definition file syntax: http://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html
And you can interpret the definition like this:
const request: there is a constant named request that implements the interface request.RequestAPI (which is also callable directly and then for that) takes arguments options of type (request.UriOptions & request.CoreOptions) | (request.UrlOptions & request.CoreOptions) and an optional parameter callback (hence the ? of type request.RequestCallback. The function returns a request.Request.
& usually mean and
A pipe | usually means or, there is no duplication URI vs URL
=> "returns"
You see request in front of everything because it's the namespace (my wording may be off here)
The definitions of UriOptions, UrlOptions, CoreOptions are buried a bit. I'm not a node user, so I don't know what you can pass to request.
For example UrlOptions can either be a string argument named "url" or a url (from require('url')). See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/request/index.d.ts#L162

How to get the class name when running a constructor function in duktape?

I'd like to use a single duktape/C constructor function as dispatcher for these kind of calls. When the dispatcher function is called I need to know for which class this happend to call the appropriate C++ construction function.
I guess the this binding won't help since it represents the (not yet fully initialized) JS object we are creating here.
Another option would be the current function, but from the docs I can't see how to get the class name from that. What else could I use?
Could you elaborate what you mean by "class name"? Do you mean the .name property of the Ecmascript function object which is used as a the 'new' target?
If so, you can use duk_is_constructor_call() to see if the current call is a constructor call, then use duk_push_current_function() to get access to the Ecmascript constructor function object, and then read its properties using the usual property API calls. For example, if by "class name" you mean .name of the function object, you'd just read its "name" property using duk_get_prop_string().

Directly calling a function bound to the exports object

I'm reading the cluster.js file of the cluster package and this part confuses me:
fs.readdirSync(__dirname + '/plugins').forEach(function(plugin){
plugin = plugin.replace('.js', '');
exports.__defineGetter__(plugin, function(){
return require('./plugins/' + plugin);
});
});
I know that you can bind objects or functions to the exports object to expose them to different files, but it seems that it is calling a function already bound to the object. However, I always thought you needed to require the file and access functions that way. What is going on here?
This is realization of lazy loading for plugins. Plugin will be loaded only after first access to module property with his name. __defineGetter__ is the 'syntax sugar' not presented in ECMAScript standard. It binds an object's property to a function to be called when that property is looked up.
If a module sets exports to a single function rather than an arbitrary object, then the result of require will be a function reference which can be called directly (note that a function is actually a type of object and as such can have properties, which can also be functions).
That's not what's going on here, though. At the time the code you've shown is executed, a function called __defineGetter__ has already been defined and attached to exports. Here it's simply being called as a method of exports (presumably because the author didn't feel the need to create a redundant local name for it).
i.e. somewhere along the line there's something like
exports.__defineGetter__ = function(propname, getter) {
...
}
Since it doesn't have a local name, the only way to call it is through exports.
Obviously the purpose of the code here is to allow you to call cluster.nameOfPlugin.method(...) without having to manually require each plugin, while not requiring all the possible plugins to be preloaded; instead only the ones you actually use get loaded.

GroovyCodeVisitor says java method returns Object instead of actual type. No generics involved

I am using a GroovyCodeVisitor to check and complain if banned types are within a script. The visitor sees attempts to new Banned() but when visiting a method that returns Banned it only sees Object. Is this intentional or am i missing something. I just want to confirm the method is a plain vanilla declaration that returns plain old boring Banned and does not included generics in any form which should eliminate erasure as a potential source of the problem.
Before any jumps, I am not using SecureASTCompilationCustsomer because i am using a matcher to ban classes rather than simply adding black and white lists and other stuff which are supported by SecureASTCompilationCustomizer.
GroovyCodeVisitor is for visiting "code". That is for example the body of a Method. The return type of a method is stored in the MethodNode, not the code part you handle with your GroovyCodeVisitor. You could use ClassCodeVisitorSupport and override visitMethod(MethodNode mn) to then get the return type via mn.getReturnType() and give that into your checking code. Of course a method's return type is only for example "Banned" if declared so. If you use "def" for example the return type will be Object.

Resources