Skip to content

Commit 3f22b88

Browse files
committed
feat: Rename PropertyInfo#partial to #optional
BREAKING CHANGE: `ObjectType#propsInfo` elements had a `partial` property. This is renamed to `optional`, because that better reflects its meaning.
1 parent 8e41a1b commit 3f22b88

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export type Properties = Record<string, Type<any>>;
232232
* Information about a single property of an object-like type including its optionality.
233233
*/
234234
export type PropertyInfo<T extends Type<unknown> = Type<unknown>> = {
235-
partial: boolean;
235+
optional: boolean;
236236
type: T;
237237
};
238238

src/types/interface.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe(object, () => {
114114
testTypes('type of keys and props', () => {
115115
const { keys, props, propsInfo } = MyType;
116116
assignableTo<{ s: typeof string; n: typeof number }>(props);
117-
assignableTo<{ s: { partial: boolean; type: typeof string }; n: { partial: boolean; type: typeof number } }>(propsInfo);
117+
assignableTo<{ s: { optional: boolean; type: typeof string }; n: { optional: boolean; type: typeof number } }>(propsInfo);
118118
assignableTo<typeof props>({ n: number, s: string });
119119
assignableTo<ReadonlyArray<'s' | 'n'>>(keys);
120120
assignableTo<typeof keys>(['s', 'n']);
@@ -139,9 +139,9 @@ describe(object, () => {
139139
expect(MyTypeWithOptional.name).toBe('{ s: string, n?: number, b?: boolean }');
140140
expect(MyTypeWithOptional.props).toStrictEqual({ s: string, n: number, b: boolean });
141141
expect(MyTypeWithOptional.propsInfo).toStrictEqual({
142-
s: { type: string, partial: false },
143-
n: { type: number, partial: true },
144-
b: { type: boolean, partial: true },
142+
s: { type: string, optional: false },
143+
n: { type: number, optional: true },
144+
b: { type: boolean, optional: true },
145145
});
146146

147147
const Alternative = MyType.mergeWith(partial({ n: number, b: boolean }));
@@ -188,14 +188,14 @@ describe(object, () => {
188188
assignableTo<{ s: typeof string; n: typeof number }>(props);
189189
assignableTo<typeof props>({ n: number, s: string, b: boolean });
190190
assignableTo<{
191-
s: { partial: boolean; type: typeof string };
192-
n: { partial: boolean; type: typeof number };
193-
b: { partial: boolean; type: typeof boolean };
191+
s: { optional: boolean; type: typeof string };
192+
n: { optional: boolean; type: typeof number };
193+
b: { optional: boolean; type: typeof boolean };
194194
}>(propsInfo);
195195
assignableTo<typeof propsInfo>({
196-
s: { partial: false, type: string },
197-
n: { partial: false, type: number },
198-
b: { partial: true, type: boolean },
196+
s: { optional: false, type: string },
197+
n: { optional: false, type: number },
198+
b: { optional: true, type: boolean },
199199
});
200200
assignableTo<MyTypeWithOptional>({ s: 'asdf' });
201201
assignableTo<MyTypeWithOptional>({ n: 123, s: 'asdf' });

src/types/interface.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class InterfaceType<Props extends Properties, ResultType>
147147
const { strictMissingKeys } = this.options;
148148
const constructResult: Record<string, unknown> = {};
149149
const details: MessageDetails[] = [];
150-
for (const [key, { type: innerType, partial }] of this.propsArray) {
150+
for (const [key, { type: innerType, optional: partial }] of this.propsArray) {
151151
const missingKey = !hasOwnProperty(input, key);
152152
if (partial) {
153153
if (missingKey || (!strictMissingKeys && input[key] === undefined)) {
@@ -282,8 +282,8 @@ define(InterfaceType, 'basicType', 'object');
282282
define(InterfaceType, 'createAutoCastAllType', function (this: InterfaceType<Properties, any>) {
283283
const name = extensionName(this, 'autoCastAll');
284284
const props: PropertiesInfo = {};
285-
for (const [key, { type, partial }] of this.propsArray) {
286-
props[key] = { type: type.autoCastAll, partial };
285+
for (const [key, { type, optional: partial }] of this.propsArray) {
286+
props[key] = { type: type.autoCastAll, optional: partial };
287287
}
288288
return createType(new InterfaceType(props, { ...this.options, name }).autoCast);
289289
});
@@ -322,21 +322,21 @@ export function partial<Props extends Properties>(
322322
return createType(new InterfaceType(toPropsInfo(props, true), options));
323323
}
324324

325-
function toPropsInfo<Props extends Properties>(props: Props, partial: boolean): PropertiesInfo<Props> {
326-
const genericPropsInfo: PropertiesInfo = mapValues(props, type => ({ type, partial }));
325+
function toPropsInfo<Props extends Properties>(props: Props, optional: boolean): PropertiesInfo<Props> {
326+
const genericPropsInfo: PropertiesInfo = mapValues(props, type => ({ type, optional }));
327327
return genericPropsInfo as PropertiesInfo<Props>;
328328
}
329329

330-
function getPossibleDiscriminators([key, { type, partial }]: [
330+
function getPossibleDiscriminators([key, { type, optional }]: [
331331
string,
332332
PropertyInfo<Type<unknown> | ObjectType<unknown>>,
333333
]): PossibleDiscriminator[] {
334-
if (!partial && 'possibleDiscriminators' in type) {
334+
if (!optional && 'possibleDiscriminators' in type) {
335335
return type.possibleDiscriminators.map(({ path, values }) => ({ path: [key, ...path], values }));
336336
}
337337
if (type.enumerableLiteralDomain) {
338338
const values = [...type.enumerableLiteralDomain];
339-
if (partial && !values.includes(undefined)) values.push(undefined);
339+
if (optional && !values.includes(undefined)) values.push(undefined);
340340
return [{ path: [key], values }];
341341
}
342342
return [];

src/types/intersection.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ describe(intersection, () => {
134134
assignableTo<{ a: typeof number; b: typeof number; c: typeof boolean }>(props);
135135
assignableTo<typeof props>({ a: number, b: number, c: boolean });
136136
assignableTo<{
137-
a: { partial: boolean; type: typeof number };
138-
b: { partial: boolean; type: typeof number };
139-
c: { partial: boolean; type: typeof boolean };
137+
a: { optional: boolean; type: typeof number };
138+
b: { optional: boolean; type: typeof number };
139+
c: { optional: boolean; type: typeof boolean };
140140
}>(propsInfo);
141141
assignableTo<typeof propsInfo>({
142-
a: { partial: false, type: number },
143-
b: { partial: false, type: number },
144-
c: { partial: false, type: boolean },
142+
a: { optional: false, type: number },
143+
b: { optional: false, type: number },
144+
c: { optional: false, type: boolean },
145145
});
146146
});
147147
});

src/types/intersection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function combinedName(types: readonly BaseObjectLikeTypeImpl<unknown>[]) {
147147
const collectedProps: PropertiesInfo = {};
148148
for (const { propsInfo } of types) {
149149
for (const [key, prop] of Object.entries(propsInfo)) {
150-
if (!collectedProps[key] || (collectedProps[key]?.partial && !prop.partial)) {
150+
if (!collectedProps[key] || (collectedProps[key]?.optional && !prop.optional)) {
151151
collectedProps[key] = prop;
152152
}
153153
}

src/types/union.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ function analyzePropsInfo<Types extends OneOrMore<BaseTypeImpl<unknown> | BaseOb
132132
for (const nextType of rest) {
133133
if ('propsInfo' in nextType) {
134134
for (const [missingKey, propInfo] of Object.entries(result).filter(([key]) => !(key in nextType.propsInfo))) {
135-
propInfo.partial || (result[missingKey] = { ...propInfo, partial: true });
135+
propInfo.optional || (result[missingKey] = { ...propInfo, optional: true });
136136
}
137137
for (const [key, { type }] of Object.entries(nextType.propsInfo)) {
138138
const existing = result[key];
139-
result[key] = { partial: existing?.partial ?? true, type: existing?.type.or(type) ?? type };
139+
result[key] = { optional: existing?.optional ?? true, type: existing?.type.or(type) ?? type };
140140
}
141141
} else {
142142
return {};

src/utils/print-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export function defaultObjectRep(propsInfo: PropertiesInfo): string {
157157
return '{}';
158158
}
159159

160-
return `{ ${props.map(([key, { partial, type }]) => `${printKey(key)}${partial ? '?' : ''}: ${type.name}`).join(', ')} }`;
160+
return `{ ${props.map(([key, { optional: partial, type }]) => `${printKey(key)}${partial ? '?' : ''}: ${type.name}`).join(', ')} }`;
161161
}
162162

163163
export function bracketsIfNeeded(name: string, allowedSeparator?: '|' | '&'): string {

0 commit comments

Comments
 (0)