Skip to content

Commit

Permalink
Simplify TupleType
Browse files Browse the repository at this point in the history
  • Loading branch information
daanboer committed May 28, 2022
1 parent 799b6bf commit 06c7d10
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
28 changes: 3 additions & 25 deletions src/Type/TupleType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ function normalize(types: Readonly<Array<BaseType | undefined>>): Array<BaseType
for (const type of types) {
if (type instanceof RestType) {
const inner_type = derefType(type.getType()) as ArrayType | InferType | TupleType;
if (inner_type instanceof InferType) {
throw new Error("Found inferred rest type when normalizing tuple types.");
}
normalized = [
...normalized,
...(inner_type instanceof ArrayType ? [type] : normalize(inner_type.getTypes())),
...(inner_type instanceof TupleType ? normalize(inner_type.getTypes()) : [type]),
];
} else {
normalized.push(type);
Expand All @@ -27,25 +24,10 @@ function normalize(types: Readonly<Array<BaseType | undefined>>): Array<BaseType
export class TupleType extends BaseType {
private types: Readonly<Array<BaseType | undefined>>;

public constructor(types: Array<BaseType | undefined>) {
public constructor(types: Readonly<Array<BaseType | undefined>>) {
super();

let resolved_types: Array<BaseType | undefined> = [];

types.forEach((type) => {
if (type instanceof RestType) {
const inner_type = type.getType();
if (inner_type instanceof TupleType) {
resolved_types = resolved_types.concat(inner_type.getTypes());
} else {
resolved_types.push(type);
}
} else {
resolved_types.push(type);
}
});

this.types = resolved_types;
this.types = normalize(types);
}

public getId(): string {
Expand All @@ -55,8 +37,4 @@ export class TupleType extends BaseType {
public getTypes(): Readonly<Array<BaseType | undefined>> {
return this.types;
}

public getNormalizedTypes(): Array<BaseType | undefined> {
return normalize(this.types);
}
}
2 changes: 1 addition & 1 deletion src/TypeFormatter/TupleTypeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TupleTypeFormatter implements SubTypeFormatter {
}

public getDefinition(type: TupleType): Definition {
const subTypes = type.getNormalizedTypes().filter(notUndefined);
const subTypes = type.getTypes().filter(notUndefined);

const requiredElements = subTypes.filter((t) => !(t instanceof OptionalType) && !(t instanceof RestType));
const optionalElements = subTypes.filter((t): t is OptionalType => t instanceof OptionalType);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/isAssignableTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export function isAssignableTo(
if (source instanceof ArrayType) {
return isAssignableTo(targetItemType, source.getItem(), inferMap, insideTypes);
} else if (source instanceof TupleType) {
return isAssignableTo(targetItemType, new UnionType(source.getNormalizedTypes()), inferMap, insideTypes);
return isAssignableTo(targetItemType, new UnionType(source.getTypes()), inferMap, insideTypes);
} else {
return false;
}
Expand Down

0 comments on commit 06c7d10

Please sign in to comment.