Import text file with ts-node - node.js

I'd like to import a text file:
import text from "./text.txt";
I have a file with:
declare module "*.txt" {
const content: string;
export default content;
}
However, ts-node complains with:
error TS2307: Cannot find module './text.txt' or its corresponding type declarations.
Please note that I cannot use the fs module to read the file since I am also using the same code for the web frontend (where the import is resolved by the bundler).
EDIT
Trying to use the proposal in the first answer:
import text from "./text.txt!text";
with
declare module "*!text" {
const content: string;
export default content;
}
I get this error:
Cannot find module './text.txt!text' from '...'

you have to tell typescript that your module will be a string type like that:
import text from "./text.txt!text";

Related

src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations

hi I want to import a graphql file. For this i have installed babel-plugin-import-graphql package.but for some reason i am getting error
\node_modules\ts-node\src\index.ts:750
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: тип Unable to compile TypeScript:
src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations.
12 import typeDefs from "./schema.graphql";
~~~~~~~~~~~~~~~~~~
how do i do the import
import typeDefs from "./schema.graphql";
graphql.d.ts
declare module "*.graphql" {
import { DocumentNode } from "graphql";
const value: DocumentNode;
export = value;
}
.babelrc
{
"plugins": ["import-graphql"]
}
full code can be viewed here
Rename the file from src/#types/graphql.d.ts to src/#types/index.d.ts, leave the contents the same, and restart the TS server (or just reload the IDE).

Create a custom typings file

I just created a published npm package for the first time, called "Foo". I am trying to consume it in a typescript project, but none of the tutorials about how to declare modules with custom typings, are clear to me. Here are the key parts of the npm package:
news.ts
import { tdsRequest } from "../common/request";
function articles(uri) {
return tdsRequest({ method: "GET" }, uri).then(returnData => console.log(returnData, "return data"));
}
export {
articles,
};
main.ts (main export)
import * as news from "./services/news";
export default {
news
};
in the typescript project that's consuming the npm package:
import { news } from "Foo";
and in the typings file ( Foo.d.ts ) I did:
declare module "Foo" {
export {
news: Object,
};
}
I get the following errors: cannot find module news and Cannot export 'Object'. Only local declarations can be exported from a module.
You are mixing default and named exports.
You can do default export style -
main.ts:
import * as news from "./services/news";
export default {
news
};
ts project import:
import foo from "Foo";
const {news} = foo;
foo.d.ts:
declare module "Foo" {
export default {
news: Object,
};
}
Or you can do named exports:
main.ts:
import * as news from "./services/news";
export {
news
};
ts project import:
import {news} from "Foo";
foo.d.ts:
declare module "Foo" {
export const news: Object;
}
But more importantly, you should add declaration: true to your compilerOptions in tsconfig.json in your npm library.
This will generate the d.ts file for you and will save you lots of work. Then, you need to add in package.json a filed called types that will point to the main.d.ts file that will be generated for you. This will allow any project using your library + typescript to use the generated types automatically.

Difference between `import from` and `import require` in TypeScript

I use node.js and I recently decided to give TypeScript a shot, But I'm kinda confused on how modules get imported. I see two different syntax that I couldn't find out what's their difference exactly:
import * as a from 'a'; // ES6 standard to import stuff
// OR ...
import a = require('a');
Are these the same thing? and if they're not, where should I use each one of them?
import * as a from 'a'; is the new "ES6 style" import syntax (available since Typescript 1.5).
Whenever possible, this syntax should now be used.
There is one caveat though. The ES6 import syntax can only import modules (as defined by ES6) or objects (classes, interfaces, vars,... ) exported as part of a module.
Some Javascript librairies will directly export a function or class, and the corresponding definition file will typically look like this:
declare module "my-class" {
class MyClass { ... }
export = MyClass
}
In this case, the "old" import syntax is the only one that can be used
import MyClass = require("my-class");
Failure to use this syntax will result in error TS2497
Check this issue for details and a possible workaround which would be, in the previous case, to add an empty module declaration to the definition file
declare module "my-class" {
class MyClass { ... }
module MyClass {} // <=
export = MyClass
}

How to use TypeScript ambient declaration interfaces in interface declared in d.ts file

I want to do a helper in .ts file like:
class ResponseHelper implements IResponseHelper {...}
and IResponseHelper is simple .d.ts file with
import * as mongoose from 'mongoose';
import * as express from 'express'
interface IResponseHelper {
toStandartResponse(response: mongoose.Promise<any>, res: express.Response): mongoose.Promise<any>;
}
as you can see params in toStandartResponse are coming from mongoose which is ambiently declared. So this if fine but if i do so I cannot us it with 'implements' like class ResponseHelper implements IResponseHelper because I got errror 'Could not find symbol IResponseHelper in external module ResponseHelper' in other words compiler cannot see d.ts file.
If i remove import statements from d.ts. file everuthing is ok but i cannot specify response types then.
Is there any way to use it all together?
I believe when you use import in makes the file a module, so you must export any members you want visible:
export interface IResponseHelper { }
Now you can import it from other files:
import {IResponseHelper} from "./IResponseHelper";
class ResponseHelper implements IResponseHelper { }

How to import a TypeScript file from another directory?

This code works fine:
import modal = require('./modal');//modal path
class index{
constructor(){
var_modal = new modal();
}
}
export = index
If I change the require path to "../../widgets/personInfo/viewmodel", after I rebuild the solution Visual Studio gives the error:
Unable to resolve external module "../../widgets/personInfo/viewmodel", Build: Module cannot be aliased to a non-module type, Invalid 'new' expression
The problem was found. Folder name was starting with uppercase, while require path was all-lowercase.
Valid code:
import modal = require('../../Widgets/PersonInfo/viewmodel')

Resources