Categories
TypeScript Answers

How to create nested classes in TypeScript?

Sometimes, we want to create nested classes in TypeScript.

In this article, we’ll look at how to create nested classes in TypeScript.

How to create nested classes in TypeScript?

To create nested classes in TypeScript, we can create a static class property in our class.

For instance, we write

class Foo {
  static Bar = class {};
}

const foo = new Foo();
const bar = new Foo.Bar();

to create the Foo class that has the static Bar property which is set to a class expression.

Then we can create a Bar instance with

const bar = new Foo.Bar();

Conclusion

To create nested classes in TypeScript, we can create a static class property in our class.

Categories
TypeScript Answers

How to use Node.js Mongoose with TypeScript?

To use Node.js Mongoose with TypeScript, we can create interfaces that extends interfaces provided by Mongoose.

For instance, we write

export interface IUser extends mongoose.Document {
  name: string;
  somethingElse?: number;
}

export const UserSchema = new mongoose.Schema({
  name: { type: String, required: true },
  somethingElse: Number,
});

const User = mongoose.model<IUser>("User", UserSchema);
export default User;

to create the IUser interface that extends the mongoose.Document interface.

And then we use it to create our model with mongoose.model<IUser>.

Therefore, the User has the name and the optional somethingElse properties.

Conclusion

To use Node.js Mongoose with TypeScript, we can create interfaces that extends interfaces provided by Mongoose.

Categories
TypeScript Answers

How to use Node.js require inside a TypeScript file?

Sometimes, we want to use Node.js require inside a TypeScript file.

In this article, we’ll look at how to use Node.js require inside a TypeScript file.

How to use Node.js require inside a TypeScript file?

To use Node.js require inside a TypeScript file, we can use the import keyword.

For instance, we write

import sampleModule = require("modulename");

or

import * as sampleModule from "modulename";

to import the modulename module in TypeScript.

Then we can compile our TypeScript project with the --module commonjs option.

Conclusion

To use Node.js require inside a TypeScript file, we can use the import keyword.

Categories
TypeScript Answers

How to fix the “Cannot find module” error with importing images in TypeScript React?

Sometimes, we want to fix the "Cannot find module" error with importing images in TypeScript React.

In this article, we’ll look at how to fix the "Cannot find module" error with importing images in TypeScript React.

How to fix the "Cannot find module" error with importing images in TypeScript React?

To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the .jpg module in a TypeScript type definition file.

For instance, we write

declare module '*.jpg';

in index.d.ts in the TypeScript project folder to let the TypeScript compiler know that .jpg files can imported into our project directly.

Conclusion

To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the .jpg module in a TypeScript type definition file.

Categories
TypeScript Answers

How to check against custom type with ‘typeof’ in TypeScript?

Sometimes, we want to check against custom type with ‘typeof’ in TypeScript.

In this article, we’ll look at how to check against custom type with ‘typeof’ in TypeScript.

How to check against custom type with ‘typeof’ in TypeScript?

To check against custom type with ‘typeof’ in TypeScript, we can use typeof to get the type of any variable.

For instance, we write

const fruit = ["apple", "banana", "grape"] as const;
type Fruit = typeof fruit[number];
const isFruit = (x: any): x is Fruit => fruit.includes(x);

const myFruit = "pear";
if (isFruit(myFruit)) {
  //...
}

to define the Fruit type by creating a union of the values in the fruit array with typeof fruit[number].

Then we create the isFruit type guard that check if x is of type Fruit with fruit.includes(x).

And then we call isFruit with myFruit to check if myFruit is a Fruit.

Since 'pear' isn’t in fruit, it’s not a Fruit.

Conclusion

To check against custom type with ‘typeof’ in TypeScript, we can use typeof to get the type of any variable.