For typing array, we need to give the type as below. In this below example the array can contain type of string only.
constnames:string[]=["Rutvik","Rohit","Virat"];names.push("Bumrah");// no error
We can also use a readonly keyword, which prevents the array been changed.
constnames:readonlystring[]=["Rutvik","Rohit","Virat"];names.push("Bumrah");// Error: Property 'push' does not exist on type 'readonly string[]'.
Question - What is Type Inference in array?
If we don’t give any type to an array, it will infer the type automatically.
constnumbers=[1,2,3];// inferred to type number[]numbers.push(4);// no error
Question - What are tuples?
It is a type array with pre-defined length and types.
It is very useful in giving types of mixed array with different types.
letourTuple:[number,boolean,string];// initialize correctlyourTuple=[5,false,'Coding Hero was here'];
Question - What are readonly tuples?
If we don’t make a tuple readonly, we can add more items to the one defined and TypeScript will not throw any error.
letourTuple:[number,boolean,string];// initialize correctlyourTuple=[5,false,'Coding Hero was here'];//No safety in indexes from 3ourTuple.push('This is wrong');
Now, to fix it we use the keyword readonly before the type.
letourTuple:readonly[number,boolean,string];// initialize correctlyourTuple=[5,false,'Coding Hero was here'];// throws error as it is readonlyourTuple.push('Coding Hero took a day off');
Question - How to give the types for Objects?
We can give the type of object by creating another object like structure and specifying the keys and the type of the keys in the object.
enumDirection{Up="Up",Down="Down",Left="Left",Right="Right"}console.log(Direction.Up);// Upconsole.log(Direction.Down);// Down
Question - What are Type Aliases?
They allow to define type with a custom name and can be used for all primitive types like string and number and also complex type like objects and arrays.
Use case: To ensure that an object’s properties cannot be modified.
interfaceUser{id:number;name:string;}constreadonlyUser:Readonly<User>={id:1,name:"Rutvik",};// readonlyUser.id = 2; // Error: Cannot assign to 'id' because it is a read-only property.
4. Pick
Creates a type by picking a set of properties K from type T.
Use case: When you need only specific properties from a type.
Constructs a type with keys K and values of type T.
Use case: To create an object type with fixed keys and consistent value types.
typeRoles="admin"|"user"|"guest";constroleDescriptions:Record<Roles,string>={admin:"Has full access",user:"Can view and edit own data",guest:"Can only view public data",};
7. Exclude
Excludes from type T all types that are assignable to U.
Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.
Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.
Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.
A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!
On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.
Top comments (0)