https://github.com/devhasanmia/assignment-1
https://github.com/devhasanmia/assignment-1
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/devhasanmia/assignment-1
- Owner: devhasanmia
- Created: 2025-05-07T17:45:44.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-07T18:42:53.000Z (about 1 year ago)
- Last Synced: 2025-05-07T19:39:12.427Z (about 1 year ago)
- Language: TypeScript
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Assalamu Alaikum, how are you all?
I hope everyone is doing well.
Today we will discuss interfaces and types.
Let's get started without delay.
## What are some differences between interfaces and types in TypeScript?
In TypeScript, both Interfaces and Types are used to define type definitions for objects, functions, or variables. Their primary purpose is to define structural types in the type system, reducing type-related errors in code and making the code more readable and maintainable.
# For Example:
````
// Interface
interface Person {
name: string;
dataOfBirth: number;
address: string;
}
// Type
type Person = {
name: string;
age: number;
address: string;
};
````
Now let's see when to use which one.
## Interfaces:
Useful only for defining object structures.
It is not suitable for defining complex types (such as unions, tuples).
However TypeScript merges interfaces with the same name if they are declared multiple times. This is very useful in large projects where modules are extended through separate declarations.
## Types:
Types are more useful for defining complex types, such as union types, tuples, function types, etc.
but Type declaration merging is not supported. Declaring types with the same name will result in an error.
Thank you.
# Explain the difference between any, unknown, and never types in TypeScript.
The types any, unknown, and never are used in various ways in TypeScript. Let's explain them in simple to understand their uses and differences.
### any Type
Using the any type in TypeScript disables the compiler from checking the type. This allows any type of data to be assigned to a variable and any operation to be performed on that variable. This provides flexibility, but it removes the type safety benefits of TypeScript. Therefore, you should be careful when using any.
````
let value: any;
value = 42;
value = "hello";
value.toFixed();
````
### unknown Type
The unknown type can accept any value, just like any. However, unlike any, you cannot directly perform operations on a value of type unknown without first ensuring its type. This type-checking requirement makes unknown much safer and less error-prone.
### never Type
The never type in TypeScript is a special type that represents values or functions that are impossible to produce or complete. It signifies that some operations or code paths will never yield a value. Understanding its purpose is crucial for advanced type safety and handling edge cases in TypeScript.
Everyone stay well and healthy, we'll talk later, one day, Inshallah.