First, let to see what means "and" and "or" of type.
interface aa {
name: string;
width: number;
};
interface bb {
name: string;
height: number;
};
let cc: aa | bb = {
name: 'jiang',
width: 20,
}
Here interfaces aa and bb are used as type, and cc run smoothly. Now how about cc used as aa & bb. No, there is error. For aa & bb, it must be the following
let dd: aa & bb = {
name: 'jiang',
width: 20,
height: 80,
}
Now the following type is clear.
1 declare type ExtendableTypes = 'Editor' | 'Element' | 'Text' | 'Selection' | 'Range' | 'Point' | 'InsertNodeOperation' | 'InsertTextOperation' | 'MergeNodeOperation' | 'MoveNodeOperation' | 'RemoveNodeOperation' | 'RemoveTextOperation' | 'SetNodeOperation' | 'SetSelectionOperation' | 'SplitNodeOperation';
The meaning of Editor Element and Text will be discussed in other article.
2 type ExtendedType<K extends ExtendableTypes, B> = unknown extends CustomTypes[K] ? B : CustomTypes[K];
This type means an unknown type which has the property that, first, to judge whether B is exists, and then, if B exists, it is equal to B. For example, the following express can work. let aa: ExtendedType<K extends ExtendableTypes, number> = 10, and const bb: ExtendedType<K extends ExtendableTypes, string> = 'jiang'.
But if B does not exist, this type will be CustomTypes[K], and what is the code of CustomTypes[K].
3 interface CustomTypes {
[key: string]: unknown;
}
It means a type of a object of many key-value but the key must be string and the value is unknown. For example
ee: CustomTypes = {name: 'jiang', age: 20, sex: 'boy'}.
What is meaning of CustomTypes[K]? At first, keyof and T[K] must be understood. Take example of interface aa
interface aa {name: string; width: number}. keyof aa is equal to 'name' or 'width'. And the following expression is right, type ee = keyof aa and let ff: ee = 'name' or let ff: ee = 'width'.
For expression T[K], K must be keyof T. The following expression is right, let gg: aa['name'] = 'jiang' or let gg: aa['width'] = 10.
Now let focus on CustomTypes[K], and K is extension of ExtendableTypes, so CustomTypes[K] will be one or more of 'Editor', 'Element', 'Text' and so on, ant that depends on what is K. Here is the most important example of slate
4 export interface BaseEditor {
children: Descendant[];
...
}
export declare type Editor = ExtendedType<'Editor', BaseEditor>;
Now, when editor.someproperty is used, the property of Editor instance firstly depends the property of BaseEditor, if BaseEditor does not has the property, it depends that of 'Editor' the custom defined. For example,
type CustomElement = { type: 'paragraph'; children: CustomText[] }
type CustomText = { text: string }
declare module 'slate' {
interface CustomTypes {
Editor: BaseEditor & ReactEditor & HistoryEditor
Element: CustomElement
Text: CustomText
}
}
According to this interface definition, it will also check the properties of ReactEditor and HistoryEditor.
5 There are also other important applications of ExtendedTypes[K].
export interface BaseElement {children: Descendant[];}
export declare type Element = ExtendedType<'Element', BaseElement>;
export interface BaseText {text: string;}
export declare type Text = ExtendedType<'Text', BaseText>;
export declare type BaseSelection = Range | null;
export declare type Selection = ExtendedType<'Selection', BaseSelection>;
export interface BaseRange {anchor: Point; focus: Point;}
export declare type Range = ExtendedType<'Range', BaseRange>;
export interface BasePoint {path: Path; offset: number;}
export declare type Point = ExtendedType<'Point', BasePoint>;
Slate ExtendedTypes[K] Analysis
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- General New Features Xcode 11 beta supports development w...