Skip to content

Commit

Permalink
tree: Cleanup unused type utils (microsoft#22592)
Browse files Browse the repository at this point in the history
## Description

Cleanup unused type utils.
  • Loading branch information
CraigMacomber authored Sep 23, 2024
1 parent 8be73d3 commit 0702400
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 177 deletions.
3 changes: 0 additions & 3 deletions packages/dds/tree/src/feature-libraries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ export {
FlexMapNodeSchema,
FlexObjectNodeSchema,
schemaIsLeaf,
schemaIsMap,
schemaIsObjectNode,
type Unenforced,
type AllowedTypeSet,
markEager,
Expand All @@ -140,7 +138,6 @@ export {
type FlexListToUnion,
type ExtractItemType,
isLazy,
type FlexObjectNodeFields,
intoStoredSchema,
intoStoredSchemaCollection,
type NormalizeLazyItem,
Expand Down
5 changes: 0 additions & 5 deletions packages/dds/tree/src/feature-libraries/typed-schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
export {
type FlexTreeNodeSchema,
FlexFieldSchema,
allowedTypesToTypeSet,
type FlexAllowedTypes,
type LazyTreeNodeSchema,
LeafNodeSchema,
Expand All @@ -18,12 +17,8 @@ export {
type FlexMapFieldSchema,
type SchemaCollection,
TreeNodeSchemaBase,
type FlexObjectNodeFields,
schemaIsLeaf,
schemaIsMap,
schemaIsObjectNode,
intoStoredSchema,
allowedTypesSchemaSet,
intoStoredSchemaCollection,
} from "./typedTreeSchema.js";

Expand Down
29 changes: 0 additions & 29 deletions packages/dds/tree/src/feature-libraries/typed-schema/typeUtils.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ import {
type requireAssignableTo,
filterIterable,
brand,
objectToMap,
} from "../../util/index.js";
import { FieldKinds } from "../default-schema/index.js";
import type { FlexFieldKind, FullSchemaPolicy } from "../modular-schema/index.js";

import type { LazyItem } from "./flexList.js";
import { type ObjectToMap, objectToMapTyped } from "./typeUtils.js";

/**
*/
export interface FlexObjectNodeFields {
interface FlexObjectNodeFields {
readonly [key: string]: FlexFieldSchema;
}

Expand Down Expand Up @@ -74,15 +74,15 @@ export abstract class TreeNodeSchemaBase<const out Specification = unknown> {

/**
*/
export class FlexMapNodeSchema extends TreeNodeSchemaBase {
export class FlexMapNodeSchema extends TreeNodeSchemaBase<FlexMapFieldSchema> {
public get mapFields(): FlexMapFieldSchema {
return this.info as FlexMapFieldSchema;
return this.info;
}

protected _typeCheck2?: MakeNominal;
public static create<const Name extends string>(
public static create(
builder: Named<string>,
name: TreeNodeSchemaIdentifier<Name>,
name: TreeNodeSchemaIdentifier,
specification: FlexMapFieldSchema,
): FlexMapNodeSchema {
return new FlexMapNodeSchema(
Expand All @@ -94,7 +94,7 @@ export class FlexMapNodeSchema extends TreeNodeSchemaBase {
}

public override getFieldSchema(field: FieldKey): FlexMapFieldSchema {
return this.info as FlexMapFieldSchema;
return this.info;
}
}

Expand Down Expand Up @@ -123,8 +123,7 @@ export class FlexObjectNodeSchema extends TreeNodeSchemaBase {
specification: FlexObjectNodeFields,
): FlexObjectNodeSchema {
const objectNodeFieldsObject = normalizeStructFields(specification);
const objectNodeFields: ObjectToMap<FlexObjectNodeFields, FieldKey, FlexFieldSchema> =
objectToMapTyped(objectNodeFieldsObject);
const objectNodeFields = objectToMap<FieldKey, FlexFieldSchema>(objectNodeFieldsObject);
return new FlexObjectNodeSchema(
builder,
name,
Expand Down Expand Up @@ -168,9 +167,7 @@ export class FlexObjectNodeSchema extends TreeNodeSchemaBase {
*/
export type FlexTreeNodeSchema = TreeNodeSchemaBase;

function normalizeStructFields<T extends FlexObjectNodeFields>(
fields: T,
): FlexObjectNodeFields {
function normalizeStructFields(fields: FlexObjectNodeFields): FlexObjectNodeFields {
const out: Record<string, FlexFieldSchema> = {};
// eslint-disable-next-line no-restricted-syntax
for (const key in fields) {
Expand Down Expand Up @@ -363,7 +360,7 @@ export type AllowedTypeSet = ReadonlySet<FlexTreeNodeSchema>;
/**
* Convert {@link FlexAllowedTypes} to {@link TreeTypeSet}.
*/
export function allowedTypesSchemaSet(t: FlexAllowedTypes): AllowedTypeSet {
function allowedTypesSchemaSet(t: FlexAllowedTypes): AllowedTypeSet {
const list: FlexTreeNodeSchema[] = t.map((value: LazyItem<FlexTreeNodeSchema>) => {
if (typeof value === "function") {
return value();
Expand All @@ -376,7 +373,7 @@ export function allowedTypesSchemaSet(t: FlexAllowedTypes): AllowedTypeSet {
/**
* Convert {@link FlexAllowedTypes} to {@link TreeTypeSet}.
*/
export function allowedTypesToTypeSet(t: FlexAllowedTypes): TreeTypeSet {
function allowedTypesToTypeSet(t: FlexAllowedTypes): TreeTypeSet {
const list = allowedTypesSchemaSet(t);
const names = Array.from(list, (type) => {
assert(type instanceof TreeNodeSchemaBase, 0x7bf /* invalid allowed type */);
Expand Down Expand Up @@ -446,27 +443,9 @@ export interface SchemaCollection {
readonly nodeSchema: ReadonlyMap<TreeNodeSchemaIdentifier, FlexTreeNodeSchema>;
}

// These schema type narrowing functions are preferred over `instanceof` due to being easier to migrate to class based schema.

/**
* Checks if a {@link FlexTreeNodeSchema} is a {@link FlexMapNodeSchema}.
*/
export function schemaIsMap(schema: FlexTreeNodeSchema): schema is FlexMapNodeSchema {
return schema instanceof FlexMapNodeSchema;
}

/**
* Checks if a {@link FlexTreeNodeSchema} is a {@link LeafNodeSchema}.
*/
export function schemaIsLeaf(schema: FlexTreeNodeSchema): schema is LeafNodeSchema {
return schema instanceof LeafNodeSchema;
}

/**
* Checks if a {@link FlexTreeNodeSchema} is a {@link FlexObjectNodeSchema}.
*/
export function schemaIsObjectNode(
schema: FlexTreeNodeSchema,
): schema is FlexObjectNodeSchema {
return schema instanceof FlexObjectNodeSchema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { FieldKinds } from "../../../feature-libraries/index.js";
import {
FlexFieldSchema,
schemaIsLeaf,
schemaIsMap,
schemaIsObjectNode,
// eslint-disable-next-line import/no-internal-modules
} from "../../../feature-libraries/typed-schema/typedTreeSchema.js";

Expand All @@ -32,18 +30,10 @@ describe("typedTreeSchema", () => {
foo: builder.optionalRecursive([() => recursiveObject]),
});

it("schema is", () => {
it("schemaIsLeaf", () => {
assert(schemaIsLeaf(getFlexSchema(booleanSchema)));
assert(!schemaIsObjectNode(getFlexSchema(booleanSchema)));
assert(!schemaIsMap(getFlexSchema(booleanSchema)));

assert(!schemaIsLeaf(emptyObjectSchema));
assert(schemaIsObjectNode(emptyObjectSchema));
assert(!schemaIsMap(emptyObjectSchema));

assert(!schemaIsLeaf(basicObjectSchema));
assert(schemaIsObjectNode(basicObjectSchema));
assert(!schemaIsMap(basicObjectSchema));
});

describe("TreeFieldSchema", () => {
Expand Down
32 changes: 0 additions & 32 deletions packages/dds/tree/src/test/util/typeUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import type {
requireTrue,
} from "../../util/index.js";
import type {
AllowOptional,
AllowOptionalNotFlattened,
OptionalFields,
RequiredFields,
RestrictiveReadonlyRecord,
RestrictiveStringRecord,
// Allow importing from this specific file which is being tested:
Expand All @@ -21,34 +17,6 @@ import type {

// These tests currently just cover the type checking, so its all compile time.

// Test OptionalFields
{
type a = OptionalFields<{ a: 5; b: undefined | 5; c: undefined }>;
type check1_ = requireAssignableTo<a, { b?: 5 }>;
type check2_ = requireAssignableTo<{ b?: 5 }, a>;
}

// Test RequiredFields
{
type a = RequiredFields<{ a: 5; b: undefined | 5; c: undefined }>;
type check1_ = requireAssignableTo<a, { a: 5 }>;
type check2_ = requireAssignableTo<{ a: 5 }, a>;
}

// Test AllowOptional
{
type a = AllowOptional<{ a: 5; b: undefined | 5; c: undefined }>;
type check1_ = requireAssignableTo<a, { a: 5; b?: 5 }>;
type check2_ = requireAssignableTo<{ a: 5; b?: 5 }, a>;
}

// Test AllowOptionalNotFlattened
{
type a = AllowOptionalNotFlattened<{ a: 5; b: undefined | 5; c: undefined }>;
type check1_ = requireAssignableTo<a, { a: 5; b?: 5 }>;
type check2_ = requireAssignableTo<{ a: 5; b?: 5 }, a>;
}

// Test RestrictiveReadonlyRecord
{
const empty = {};
Expand Down
5 changes: 0 additions & 5 deletions packages/dds/tree/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,9 @@ export {
export { ReferenceCountedBase, type ReferenceCounted } from "./referenceCounting.js";

export type {
AllowOptional,
RequiredFields,
OptionalFields,
_RecursiveTrick,
AllowOptionalNotFlattened,
RestrictiveReadonlyRecord,
RestrictiveStringRecord,
Assume,
_InlineTrick,
FlattenKeys,
} from "./typeUtils.js";
Expand Down
60 changes: 0 additions & 60 deletions packages/dds/tree/src/util/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,6 @@
*/
export type FlattenKeys<T> = [{ [Property in keyof T]: T[Property] }][_InlineTrick];

/**
* Remove all fields which permit undefined from `T`.
*/
export type RequiredFields<T> = [
{
[P in keyof T as undefined extends T[P] ? never : P]: T[P];
},
][_InlineTrick];

/**
* Extract fields which permit undefined but can also hold other types.
*/
export type OptionalFields<T> = [
{
[P in keyof T as undefined extends T[P]
? T[P] extends undefined
? never
: P
: never]?: T[P];
},
][_InlineTrick];

/**
* Converts properties of an object which permit undefined into optional properties.
* Removes fields which only allow undefined.
*
* @remarks
* This version does not flatten the resulting type.
* This version exists because some cases recursive types need to avoid this
* flattening since it causes complication issues.
*
* See also `AllowOptional`.
*/
// export type AllowOptionalNotFlattened<T> = [RequiredFields<T> & OptionalFields<T>][_InlineTrick];
export type AllowOptionalNotFlattened<T> = [
RequiredFields<T> & OptionalFields<T>,
][_InlineTrick];

/**
* Converts properties of an object which permit undefined into optional properties.
* Removes fields which only allow undefined.
*/
export type AllowOptional<T> = [
FlattenKeys<RequiredFields<T> & OptionalFields<T>>,
][_InlineTrick];

/**
* Use for trick to "inline" generic types.
*
Expand Down Expand Up @@ -183,17 +137,3 @@ export type RestrictiveStringRecord<T> = {
} & {
readonly [P in symbol]?: never;
};

/**
* Assume that `TInput` is a `TAssumeToBe`.
*
* @remarks
* This is useful in generic code when it is impractical (or messy)
* to to convince the compiler that a generic type `TInput` will extend `TAssumeToBe`.
* In these cases `TInput` can be replaced with `Assume<TInput, TAssumeToBe>` to allow compilation of the generic code.
* When the generic code is parameterized with a concrete type, if that type actually does extend `TAssumeToBe`,
* it will behave like `TInput` was used directly.
*/
export type Assume<TInput, TAssumeToBe> = [TInput] extends [TAssumeToBe]
? TInput
: TAssumeToBe;

0 comments on commit 0702400

Please sign in to comment.