avatar

TypeScript中type,interface和array

最近写type写的我脑阔疼,因为每个人的表达方式都不一样……我在下面举例几种:

这是一套用法

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
//ESignDefinitions.ts
export enum ESignRecipientType {
SIGNER = 'SIGNER',
VIEW_ONLY = 'VIEW_ONLY',
EXTERNAL = 'EXTERNAL',
CARBON_COPY = 'CARBON_COPY',
}

// 调用可直接使用 ESignRecipientType.SIGNER

// CreateEnvelopeSchema.ts
const envelopeRecipientSchema = Joi.object({
type: Joi.valid(_.values(ESignRecipientType)).required(),
partyId: Joi.string().required(),
name: Joi.string().optional(),
email: Joi.string().optional(),
roleName: Joi.string().optional(),
isExternal: Joi.boolean().required(),
}).unknown();


// eSign/types/type.d.ts
declare namespace ESign {
type RecipientTypes = 'SIGNER' | 'VIEW_ONLY' | 'CARBON_COPY';
}

declare namespace ESignCreateEnvelope {

interface IEnvelopeRecipient {
type: ESign.RecipientTypes;
partyId: string;
name?: string;
email?: string;
roleName?: string;
isExternal: boolean;
}
}

第二套:

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
41
42
43
const STATUS_TYPES = ['VIEWED', 'SIGNED', 'DECLINED', 'WAITING', 'YET_TO_SEND'] as const;
const StatusTypes = createDefinitionObject(STATUS_TYPES);
type StatusType = keyof typeof StatusTypes;

// the createDefinitionObject it used
export function createDefinitionObject<T extends string | number>(elems: readonly T[]): { [K in T]: K } {
return _.zipObject(elems, elems) as any as { [K in T]: K };
}

const SignerInfoSchema = Joi.object().keys({
type: Joi.string().valid(SIGNER_TYPES), //here
borrowerId: Joi.string().when('type', { is: SignerTypes.BORROWER, then: Joi.required() }),
name: Joi.string().required(),
email: Joi.string().required(),
clientUserId: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
docLink: Joi.string().optional(),
status: Joi.string().valid(STATUS_TYPES),
firstViewed: DateTimeSchema.optional(),
signedAt: DateTimeSchema.optional(),
declinedToSignAt: DateTimeSchema.optional(),
viewedIP: Joi.string().ip().optional(),
signedIP: Joi.string().ip().optional(),
declinedIP: Joi.string().ip().optional(),
});

// types.d.ts
type StatusTypes = import('../schemas/EnvelopeSchemas').StatusType;

interface ISigner {
type?: SignerTypes;
borrowerId?: string;
name: string;
email: string;
clientUserId: string | number;
docLink?: string;
status?: StatusTypes;
firstViewed?: number;
signedAt?: number;
declinedToSignAt?: number;
viewedIP?: string;
signedIP?: string;
declinedIP?: string;
}

这是另一套用法:

1
2
3
4
// EnvelopeSchemas.ts
// disclosures is temporary until we can safely download documents for all followUps.
const PARENT_TYPES: ESign.ParentTypes[] = ['disclosuresFollowUp', 'followUp', 'process'];
const ParentTypes = _.zipObject(PARENT_TYPES, PARENT_TYPES);

待解决:怎么实现type的向下兼容

比如我有一个type是有三个属性,另一个type有四个属性,但其中三个都是前者包含的。然后我想把第一个type传入以第二个type作为interface的函数中,如何让他们兼容

文章作者: 8128
文章链接: http://8128.me/2020/06/05/TypeScript%E4%B8%ADtype,interface%E5%92%8Carray/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 8128's Blog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论