I found this piece of code in github and I don’t understand it
function writeClients(dictionary: IReferenceDirectory[]) {
//more code here
let superClientFile = `import IClientOptions from './base/IClientOptions';n`;
superClientFile += `import Transport from './base/Transport';nn`;
why don’t the author import at the most top? What’s the intention here? and he use +=
too
2
Answers
Looks like the library dynamically builds the
TwitterClient
class with fields and methods depending on the passeddictionary
argument. At the end of the referenced file, you can see that the generated class is written to disk:So it seems that this is a fancy way of generating the
TwitterClient
that can later be imported in your app. Also building the class content using+=
concatenation is considered the way to go nowadays, see JS strings "+" vs concat method for more information.It imports the selected files based on clients.
And at the end of the file, it writes the code into the file
index.ts
so above two import lines should be included insuperClientFile
string.That is the code of file –
index.ts