Initialize Typed variable to an Empty Object in TypeScript # Use type assertions to initialize a typed variable to an empty object, e.g. const a1 = {} as Animal; . You can then set the properties on the object using dot or bracket notation. All of the properties you set on the object need to conform to the type.
To initialize an object in TypeScript, we can create an object that matches the properties and types specified in the interface. export interface Category { name: string; description: string; } const category: Category = { name: "My Category", description: "My Description", };
The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.
Here are two worthy caveats from the comments.
Either you want user to be of type
User | {}
orPartial<User>
, or you need to redefine theUser
type 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
Username
is left undefined, while the type annotation is saying it can't be undefined. –Ian Liu Rodrigues
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.
Really depends on what you're trying to do. Types are documentation in typescript, so you want to show intention about how this thing is supposed to be used when you're creating the type.
Make all attributes optional
type User = {
attr0?: number
attr1?: string
}
type User = {
...
}
let u1: User = null;
Though, really, here if the point is to declare the User object before it can be known what will be assigned to it, you probably want to do let u1:User
without any assignment.
Really, the premise of typescript is to make sure that you are conforming to the mental model you outline in types in order to avoid making mistakes. If you want to add things to an object one-by-one, this is a habit that TypeScript is trying to get you not to do.
More likely, you want to make some local variables, then assign to the User-containing variable when it's ready to be a full-on User. That way you'll never be left with a partially-formed User. Those things are gross.
let attr1: number = ...
let attr2: string = ...
let user1: User = {
attr1: attr1,
attr2: attr2
}
Note that using const user = {} as UserType
just provides intellisense but at runtime user
is empty object {}
and has no property inside. that means user.Email
will give undefined
instead of ""
type UserType = {
Username: string;
Email: string;
}
So, use class
with constructor
for actually creating objects with default properties.
type UserType = {
Username: string;
Email: string;
};
class User implements UserType {
constructor() {
this.Username = "";
this.Email = "";
}
Username: string;
Email: string;
}
const myUser = new User();
console.log(myUser); // output: {Username: "", Email: ""}
console.log("val: "+myUser.Email); // output: ""
You can also use interface
instead of type
interface UserType {
Username: string;
Email: string;
};
...and rest of code remains same.
Actually, you can even skip the constructor
part and use it like this:
class User implements UserType {
Username = ""; // will be added to new obj
Email: string; // will not be added
}
const myUser = new User();
console.log(myUser); // output: {Username: ""}
you can do this as below in typescript
const _params = {} as any;
_params.name ='nazeh abel'
since typescript does not behave like javascript so we have to make the type as any otherwise it won't allow you to assign property dynamically to an object
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> = {};
In my case Record<string, never>
helps, it was recommended by eslint
user: USER
this.user = ({} as USER)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With