Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix intersections with primitives to add support for nominal types #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/transform-inline/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ts = require('typescript');

export function sliceSet<T>(set: Set<T>): T[] {
const items: T[] = [];
set.forEach((value) => items.push(value));
Expand All @@ -17,3 +19,15 @@ export function sliceMapValues<T, U>(map: Map<T, U>): U[] {
map.forEach((value) => items.push(value));
return items;
}
export function isPrimitive(flag: number) {
return (
((ts.TypeFlags.Number |
ts.TypeFlags.String |
ts.TypeFlags.Boolean |
ts.TypeFlags.BooleanLiteral |
ts.TypeFlags.StringLiteral |
ts.TypeFlags.NumberLiteral) &
flag) !==
0
);
}
34 changes: 31 additions & 3 deletions src/transform-inline/visitor-type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as VisitorKeyof from './visitor-keyof';
import * as VisitorIndexedAccess from './visitor-indexed-access';
import * as VisitorIsStringKeyof from './visitor-is-string-keyof';
import * as VisitorTypeName from './visitor-type-name';
import { sliceSet } from './utils';
import { sliceSet,isPrimitive } from './utils';

function visitDateType(type: ts.ObjectType, visitorContext: VisitorContext) {
const name = VisitorTypeName.visitType(type, visitorContext, { type: 'type-check' });
Expand Down Expand Up @@ -501,8 +501,36 @@ function visitUnionOrIntersectionType(type: ts.UnionOrIntersectionType, visitorC
if (tsutils.isIntersectionType(intersectionType)) {
const name = VisitorTypeName.visitType(type, visitorContext, { type: 'type-check', superfluousPropertyCheck: visitorContext.options.disallowSuperfluousObjectProperties });
return VisitorUtils.setFunctionIfNotExists(name, visitorContext, () => {
const functionNames = intersectionType.types.map((type) => visitType(type, { ...visitorContext, overrideDisallowSuperfluousObjectProperies: true }));
if (disallowSuperfluousPropertyCheck) {
let functionNames: string[] = [];
if (
intersectionType.types.length === 2 &&
((intersectionType.types[0].flags & ts.TypeFlags.Object &&
isPrimitive(intersectionType.types[1].flags)) ||
(isPrimitive(intersectionType.types[0].flags) &&
intersectionType.types[1].flags & ts.TypeFlags.Object))
) {
intersectionType.types.forEach((type) => {
if (isPrimitive(type.flags)) {
functionNames.push(
visitType(
type,
Object.assign(Object.assign({}, visitorContext), {
overrideDisallowSuperfluousObjectProperies: true
})
)
);
}
});
} else {
functionNames = intersectionType.types.map((type) =>
visitType(
type,
Object.assign(Object.assign({}, visitorContext), {
overrideDisallowSuperfluousObjectProperies: true
})
)
);
} if (disallowSuperfluousPropertyCheck) {
// Check object keys at intersection type level. https://github.com/woutervh-/typescript-is/issues/21
const keys = VisitorIsStringKeyof.visitType(type, visitorContext);
if (keys instanceof Set) {
Expand Down