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 OverflowIf 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();
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.
typescript - Create instance using an interface - Stack Overflow
How to Create an Object Using an Interface in TypeScript - Ask a Question - TestMu AI Community
Terse way to create instance of interface Bar with all fields in that are also in an instance of Foo?
How do I express a map of classes and their instances?
Videos
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
You can do it that way. You can also just create an object that implements the interface like:
interface foo {
one: number;
two: string;
}
const bar: foo = { one: 5, two: "hello" };
If you want to use a class, you can put it where you want. If it's tightly coupled with the component, you can put it there. Generally though, I want classes to be loosely coupled, so I put them in their own file.
I use this way
interface IObject{
first: number;
second: string;
}
then
var myObject = {} as IObject
var myListObject = [] as Array<IObject>