Caveats
Here are two worthy caveats from the comments.
Either you want user to be of type
User | {}orPartial<User>, or you need to redefine theUsertype to allow an empty object. Right now, the compiler is correctly telling you that user is not a User. –jcalz
I don't think this should be considered a proper answer because it creates an inconsistent instance of the type, undermining the whole purpose of TypeScript. In this example, the property
Usernameis left undefined, while the type annotation is saying it can't be undefined. –Ian Liu Rodrigues
Answer
One of the design goals of TypeScript is to "strike a balance between correctness and productivity." If it will be productive for you to do this, use Type Assertions to create empty objects for typed variables.
type User = {
Username: string;
Email: string;
}
const user01 = {} as User;
const user02 = <User>{};
user01.Email = "[email protected]";
Here is a working example for you.

Caveats
Here are two worthy caveats from the comments.
Either you want user to be of type
User | {}orPartial<User>, or you need to redefine theUsertype to allow an empty object. Right now, the compiler is correctly telling you that user is not a User. –jcalz
I don't think this should be considered a proper answer because it creates an inconsistent instance of the type, undermining the whole purpose of TypeScript. In this example, the property
Usernameis left undefined, while the type annotation is saying it can't be undefined. –Ian Liu Rodrigues
Answer
One of the design goals of TypeScript is to "strike a balance between correctness and productivity." If it will be productive for you to do this, use Type Assertions to create empty objects for typed variables.
type User = {
Username: string;
Email: string;
}
const user01 = {} as User;
const user02 = <User>{};
user01.Email = "[email protected]";
Here is a working example for you.

An empty object can be written as Record<string,never>, so effectively your type for user is either an empty object or a User
const user : User | Record<string, never> = {};
I know this is a really basic question, and I've tried to look at similar questions and didn't understand it so I apologize.
I'm translating my JavaScript code to TypeScript, and have a lot of code that looks similar to this:
if (!user_dict.options) {
user_dict.options = {}
}This produces the error "property options does not exist on type {}" for both lines. How can I establish an empty object like this, if I don't know what type of variables will later be stored? I have hundreds of empty variable declarations like this, and I can't know at this point in the code the "type" of variable being stored later, so I don't understand how I'm supposed to handle this using TypeScript?
I have hundreds of empty variable declarations like this, and I can't know at this point in the code the "type" of variable being stored later,
You don't know what sort of data you're planning on putting in the variable?
"property options does not exist on type {}"
Do it basically tells you that user_dict type doesn’t contain ‘options’ property. You need to add it to user_dict type or interface. Or if you don’t have type for this object you’d better write it.
Initialize empty object in typescript with Record type - Stack Overflow
Empty object type is not working
Define an empty object type in TypeScript - Stack Overflow
How can I create an object based on an interface file definition in TypeScript? - Stack Overflow
Simply use the Partial utility type: Partial<Type>
type Plan = 'plan1' | 'plan1';
interface IPlan {
name: string
}
const plans: Partial<Record<Plan, IPlan>> = {}; // no error
plans.plan1 = {
name: 'Plan #1'
}
The downside of this approach is that now all the properties of your interface are optional. But since you want it to instantiate without the required property, that is the only way.
Playground Link
Another idea might be using the Omit utility type: Omit<Type, Keys>
interface Plan {
name: string;
}
type IPlan = Omit<Plan , "name">;
const plans: IPlan = {};
So, again, you can instantiate without the required properties.
Playground Link
You could do something like:
type Plan = 'plan1' | 'plan2';
interface IPlan {
name: string
}
type PlansRecord = Record<Plan, Readonly<IPlan>>
const plansRecord = {} as PlansRecord
console.log({plansRecord})
Output:
[LOG]: { "plansRecord": {} }
Demo
type EmptyObject = {
[K in any] : never
}
const one: EmptyObject = {}; // yes ok
const two: EmptyObject = {a: 1}; // error
What we are saying here is that all eventual properties of our EmptyObject can be only never, and as never has no representing value, creating such property is not possible, therefor the object will remain empty, as this is the only way we can create it without compilation error.
type EmptyObject = Record<any, never>
This is equivalent to Maciej Sikora's answer but makes use of the Record utility type.
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();
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.