Is this possible with TypeScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is this possible with TypeScript?

Hi all, I was wondering if there is any way to write some code using typescript that has the same functionality as the code below. I am especially interested to find out how I could pass the app and router functions to another file without creating a new instance or explicitly importing it. Is this only possible in JS? I've tried to research it, but I can't figure out how to look it up at all. module.exports = (app)=>{ const router = require('express').Router(); require('./user')(router); require('./pet')(router); require('./ad')(router); require('./admin')(router); require('./survey')(router); app.use(router) } PS: app was declared in a different file as const app = express();

24th Dec 2020, 3:38 AM
Bryan Moreno
Bryan Moreno - avatar
4 Answers
0
By definition, it's still compiling to javascript, that means all javascript functionality is available in typescript, since typescript is nothing but a superset of javascript. If your question simply is it possible? My answer: it's.
24th Dec 2020, 2:53 PM
Jouhar
Jouhar - avatar
0
You're right, Technically I could just put that code in there and the Javascript files will work just fine, but I would have to ignore the compilation errors I get when I have tried that same code in a typescrypt file, so that's why I was wondering. I should have phrased the question as: is there a more typescrypt way of translating that code so that typescrypt doesn't get angry at me during compilation hehe?
25th Dec 2020, 1:33 AM
Bryan Moreno
Bryan Moreno - avatar
0
Haha, Well if it's about typescript doesn't get angry and you don't like it to be angry, then you don't have to use it in the first place. But since you are using it, you have to make it happy, make export default (in your case) And make types for each parameter, In that way it will not complain.
25th Dec 2020, 4:31 AM
Jouhar
Jouhar - avatar
0
And don't forget to install @types/express I think it's or tou will find it under the name of @types/expressjs.
25th Dec 2020, 4:33 AM
Jouhar
Jouhar - avatar