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> = {};
Initialize empty object in typescript with Record type - Stack Overflow
Empty object type is not working
Generic record key (string | number) doesn't allow an empty record
Empty object should default type to Record<string, unknown>
What is an empty object in TypeScript?
How to check if an object is empty in TypeScript?
Is null or empty in TypeScript?
Let's say I have
const someObject = {};
const ObjectKeys = ["a", "b", "c", "d", ... ] as const;
type ObjectKeyType = typeof ObjectKeys[number];
let initialised = false;
function init() {
// Do stuff
initialised = true;
}
After init is called, someObject always satisfies the type Record<ObjectKeyType, SomeOtherType>, but before that it's empty. I get around this by setting someObject to a partial of the record, but then that means every single time I access a property of someObject later I have to check whether or not it's undefined when really I should just have to check initialised at the start of the function and return early if it's false.
Is there a convenient way to tell typescript "Hey, if initialised is true then someObject should always be a complete record instead of only a partial one"?
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
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.