-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36.TypesVsInterfaces.ts
40 lines (32 loc) · 1.63 KB
/
36.TypesVsInterfaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Types vs Interfaces
// Interfaces are a way to define the shape of an object.
// Here, we define an interface for the properties of a form field.
export interface FormFieldProps {
fieldType: 'username' | 'password'; // The type of the form field.
content: string; // The content or value of the form field.
onContentChange: (updatedContent: string) => void; // A function that gets called when the content changes.
}
// Types can be used to define custom types.
// Here, we define types for the form field's properties.
// A type for the function that gets called when the content of the form field changes.
type OnContentChangeFunction = (updatedContent: string) => void;
// A type for the content or value of the form field.
type FormFieldContent = string;
// A type for the possible types of the form field.
type FormFieldType = 'username' | 'password';
// We can then use these types to define the shape of an object.
export type FormFieldPropsUsingTypes = {
fieldType: FormFieldType;
content: FormFieldContent;
onContentChange: OnContentChangeFunction;
}
// Notes:
// type:
// 1. Unions: Allows a value to be one of several types (e.g., 'username' | 'password').
// 2. Intersection(&): Combines multiple types into one.
// 3. Primitives: Basic types like string, number, boolean.
// 4. Shorthand functions: Define functions in a concise way.
// 5. Advanced Type functions: More complex type manipulations.
// interface:
// 1. Declaration Merging: Multiple interface declarations with the same name get merged.
// 2. Familiarity (extends): Interfaces can extend other interfaces, inheriting their properties.