If you are creating the "modal" variable elsewhere, and want to tell TypeScript it will all be done, you would use:

declare const modal: IModal;

If you want to create a variable that will actually be an instance of IModal in TypeScript you will need to define it fully.

const modal: IModal = {
    content: '',
    form: '',
    href: '',
    $form: null,
    $message: null,
    $modal: null,
    $submits: null
};

Or lie, with a type assertion, but you'll lost type safety as you will now get undefined in unexpected places, and possibly runtime errors, when accessing modal.content and so on (properties that the contract says will be there).

const modal = {} as IModal;

Example Class

class Modal implements IModal {
    content: string;
    form: string;
    href: string;
    $form: JQuery;
    $message: JQuery;
    $modal: JQuery;
    $submits: JQuery;
}

const modal = new Modal();

You may think "hey that's really a duplication of the interface" - and you are correct. If the Modal class is the only implementation of the IModal interface you may want to delete the interface altogether and use...

const modal: Modal = new Modal();

Rather than

const modal: IModal = new Modal();
Answer from Fenton on Stack Overflow
Top answer
1 of 16
762

If you are creating the "modal" variable elsewhere, and want to tell TypeScript it will all be done, you would use:

declare const modal: IModal;

If you want to create a variable that will actually be an instance of IModal in TypeScript you will need to define it fully.

const modal: IModal = {
    content: '',
    form: '',
    href: '',
    $form: null,
    $message: null,
    $modal: null,
    $submits: null
};

Or lie, with a type assertion, but you'll lost type safety as you will now get undefined in unexpected places, and possibly runtime errors, when accessing modal.content and so on (properties that the contract says will be there).

const modal = {} as IModal;

Example Class

class Modal implements IModal {
    content: string;
    form: string;
    href: string;
    $form: JQuery;
    $message: JQuery;
    $modal: JQuery;
    $submits: JQuery;
}

const modal = new Modal();

You may think "hey that's really a duplication of the interface" - and you are correct. If the Modal class is the only implementation of the IModal interface you may want to delete the interface altogether and use...

const modal: Modal = new Modal();

Rather than

const modal: IModal = new Modal();
2 of 16
284

If you want an empty object of an interface, you can do just:

var modal = <IModal>{};

The advantage of using interfaces in lieu of classes for structuring data is that if you don't have any methods on the class, it will show in compiled JS as an empty method. Example:

class TestClass {
    a: number;
    b: string;
    c: boolean;
}

compiles into

var TestClass = (function () {
    function TestClass() {
    }
    return TestClass;
})();

which carries no value. Interfaces, on the other hand, don't show up in JS at all while still providing the benefits of data structuring and type checking.

Discussions

typescript - Create instance using an interface - Stack Overflow
This approach defeats the point of type safety: zohaib.me/creating-object-based-on-interface-type-in-typescript 2018-11-12T09:28:28.493Z+00:00 ... While the accepted answer is good, beware of solutions like this because they allow you to omit required properties defined in the interface: const instance1 ... More on stackoverflow.com
🌐 stackoverflow.com
How to Create an Object Using an Interface in TypeScript - Ask a Question - TestMu AI Community
How can I create an object based on an interface definition in TypeScript? I have an interface defined as follows: interface IModal { content: string; form: string; href: string; $form: JQuery; $message: JQuery; $modal: JQuery; $submits: JQuery; } I define a variable like this: var modal: IModal; ... More on community.testmuai.com
🌐 community.testmuai.com
0
October 7, 2024
Terse way to create instance of interface Bar with all fields in that are also in an instance of Foo?
It’s important to remember that type information doesn’t exist at runtime. There’s no way to make the compiled JavaScript aware that there is a relationship between Foo and Bar. If you wanted to do something like this you’d have to set up manual boilerplate to do so. The most basic approach would be a function like this: function pickBar({ alpha, beta }: Foo): Bar { return { alpha, beta }; } There are ways you could maybe build machinery to avoid having to write your own pickX function for every type, but this would likely involve more overhead and complexity than it would be worth. More on reddit.com
🌐 r/typescript
11
11
January 17, 2023
How do I express a map of classes and their instances?
One way could be a wrapper class for the Registry that uses the key as a generic type and maintains that through the get and set operations. class A { constructor(private a: number) {} } class B { constructor(private b: boolean) {} } class C { constructor(private c: string) {} } const registryKeys = [A, B, C] as const type RegistryKey = typeof registryKeys[number] class RegistryKey any> { #data = new Map>() get>(key: K): V | undefined { return this.#data.get(key) } set>(key: K, value: V): this { this.#data.set(key, value) return this } } const registry = new Registry() Then all your test cases are working as expected. registry .set(A, new A(42)) .set(B, new B(true)) .set(C, new C("C")) .set("A", 42) // error .set(A, new C("C")) // error More on reddit.com
🌐 r/typescript
10
1
January 30, 2023
🌐
Reddit
reddit.com › r/typescript › terse way to create instance of interface bar with all fields in that are also in an instance of foo?
r/typescript on Reddit: Terse way to create instance of interface Bar with all fields in that are also in an instance of Foo?
January 17, 2023 -

In this example, I create an instance of Foo, and now I want an instance of Bar, using values from my Foo instance that are only applicable to Bar. Is there a way to do this without explicitly setting all fields?

interface Foo {
    alpha: string,
    beta: string,
    gamma: string
}

type Bar = Pick<Bar, 'alpha' | 'beta'>;

const x: Foo = {
    alpha: 'A',
    beta: 'B',
    gamma: 'G'
}

// I don't want to do this part manually
const y: Bar = {
    alpha: x.alpha,
    beta: x.beta
}

thanks in advance

🌐
Educative
educative.io › blog › typescript-interfaces
A Simple Guide to Typescript Interfaces: declaration & use cases
March 10, 2026 - Generic interfaces are written ... a type or another interface later in your code. We can create a generic interface by passing type parameters, using angle brackets (<>), to our interface....
🌐
HowToDoInJava
howtodoinjava.com › home › typescript › creating objects from interface in typescript
Creating Objects from Interface in TypeScript
April 15, 2024 - It should be remembered that interfaces exist only in the compile time, in the source code, for type safety purposes. Interfaces do not exist in the runtime and that is why we cannot use the instanceof operator with interfaces. Happy Learning !!
🌐
Medium
abhishekw.medium.com › typescript-how-to-create-and-use-a-interface-6fcf6200b55b
TypeScript: How to Create and Use a Interface | by Abhishek Wadalkar | Medium
January 22, 2025 - It includes three properties (id, ... string. interface UserInterface { id: string; // Unique identifier for the user name: string; // Name of the user age: number; // Age of the user // Method that returns a custom message getMessage(): string; } ... id, name, ...
🌐
DEV Community
dev.to › aneeqakhan › how-to-create-interfaces-in-typescript-24gp
How to create interfaces in TypeScript - DEV Community
March 18, 2022 - Interfaces and Classes are used to create custom types in TypeScript. These are similar but also have significant differences which I want to mention below. interface keyword is used to create an interface following the name of the interface.
Find elsewhere
🌐
Zohaib
zohaib.me › creating-object-based-on-interface-type-in-typescript
Creating object based on interface type in Typescript - Zohaib
August 22, 2018 - I'm learning Typescript and found different ways to create an object which conforms to the interface type but not all might have the type safe guarantee as you expect. ... Both of these methods will work. The issue arises when you try adding or removing properties from the interface Foo. ... After adding baz property you might think that compiler should throw error wherever Foo instance is being created letting you know there is a missing property.
🌐
Angular
angular.dev › tutorials › first-app › 04-interfaces
Create an interface • Angular
This tutorial lesson demonstrates how to create an interface and include it in a component of your app. Your app has a new interface that it can use as a data type. Your app has an instance of the new interface with sample data. Interfaces are custom data types for your app. Angular uses TypeScript ...
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-create-object-based-on-interface
Create an Object based on an Interface in TypeScript | bobbyhadz
To create an object based on an interface, declare the object's type to be the interface. The object has to conform to the property names and the type of the values in the interface, otherwise, the type checker throws an error.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-make-instance-of-interface
Creating Instances of Interfaces in TypeScript
Now, let's create an object that ... will enforce that the object must have name and age properties of the specified types. While we cannot directly create instances of interfaces, we can create instances of objects that match the interface's structure....
🌐
Delft Stack
delftstack.com › home › howto › typescript › typescript create object from interface
How to Create an Object From Interface in TypeScript | Delft Stack
February 2, 2024 - The as keyword forces the compiler to recognize the object is of a certain type even if all the fields in the object have not been set. interface Animal { legs : number ; eyes : number ; name : string ; wild : boolean ; }; const dog : Animal = { legs : 4, name : 'Dog', } as Animal;
🌐
Webdevtutor
webdevtutor.net › blog › typescript-return-new-instance-of-interface
Creating a New Instance of an Interface in TypeScript: A Comprehensive Guide
In this blog post, we've covered the basics of creating a new instance of an interface in TypeScript. By leveraging interfaces as blueprints for objects, you can ensure that your code remains type-safe and maintainable.
🌐
xjavascript
xjavascript.com › blog › typescript-instance-of-interface
Mastering TypeScript: Instances of Interfaces — xjavascript.com
TypeScript allows interfaces to inherit from other interfaces. This can help you reuse code and create more complex type hierarchies. interface Animal { eat(): void; } interface Mammal extends Animal { nurse(): void; } class Dog implements Mammal { eat() { console.log('The dog is eating.'); } nurse() { console.log('The dog is nursing.'); } } Understanding how to create and work with instances of interfaces is an essential skill in TypeScript.
🌐
Prismic
prismic.io › blog › typescript-interfaces
TypeScript Interfaces: A Practical Guide with Code Examples
September 28, 2023 - In this example, we have an interface called PersonInt and use the implements keyword to say the class Person will have all of the types defined in PersonInt. Because this isn’t true and the age field is missing in the class, an error is thrown. When working with objects in TypeScript, it’s quite common to have properties that might only be defined some of the time. In these instances...
🌐
Webdevtutor
webdevtutor.net › blog › typescript-new-instance-of-interface
Creating New Instance of Interface in TypeScript
Another way to create new instances of interfaces is by using object literals directly: const anotherPerson: Person = { name: "Charlie", age: 35 }; Using interfaces to define object structures brings several benefits to your TypeScript code:
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to Create an Object Using an Interface in TypeScript - Ask a Question - TestMu AI Community
October 7, 2024 - I have an interface defined as follows: interface IModal { content: string; form: string; href: string; $form: JQuery; $message: JQuery; $modal: JQuery; $submits: JQuery; } I define a variable like this: var modal: IModal; ...
🌐
Webdevtutor
webdevtutor.net › blog › typescript-create-an-instance-of-an-interface
How to Create an Instance of an Interface in TypeScript
If an interface describes the shape of a class, you can create an instance of the class that implements the interface. This is a common practice when working with classes in TypeScript.
🌐
Scaler
scaler.com › topics › typescript › interface-in-typescript
Interface in TypeScript - Scaler Topics
March 28, 2023 - TypeScript gives built-in support for the concept of class. The class keyword is used to initialize a class. When we use the class keyword in TypeScript, we are creating two things with the same identifier: A TypeScript interface containing all the instance methods and properties of a class.