Skip to content

Commit

Permalink
fix: force type alias resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 4, 2022
1 parent d5f8b76 commit 293b3bb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ function _recursiveMergeIntersection(typeChecker: ts.TypeChecker, types: ts.Type

const objectTypes: ts.ObjectType[] = []
const otherTypes: ts.Type[] = []
const functionTypes: ts.ObjectType[] = []
let res: ts.Type

for(const type of types) {
const signatures = [
...typeChecker.getSignaturesOfType(type, ts.SignatureKind.Call),
...typeChecker.getSignaturesOfType(type, ts.SignatureKind.Construct),
]

if(type.flags & ts.TypeFlags.Intersection) {
;(type as ts.IntersectionType).types.forEach((type) => {
if(type.flags & ts.TypeFlags.Object) {
Expand All @@ -24,22 +30,39 @@ function _recursiveMergeIntersection(typeChecker: ts.TypeChecker, types: ts.Type
}
})
} else if(type.flags & ts.TypeFlags.Union) {
const newType = createUnionType(typeChecker, )
const newType = createUnionType(typeChecker)
otherTypes.push(newType)
seen.set(type, newType)

const unionTypeMembers = (type as ts.UnionType).types.map(t => _recursiveMergeIntersection(typeChecker, [t], seen))
newType.types = unionTypeMembers
} else if(type.flags & ts.TypeFlags.Object) {
objectTypes.push(type as ts.ObjectType)
if(signatures.length > 0) {
// function type
otherTypes.push(type)
functionTypes.push(type as ts.ObjectType)
} else {
objectTypes.push(type as ts.ObjectType)
}
} else {
otherTypes.push(type)
}
}

if(otherTypes.length === 1 && objectTypes.length === 0) {
if(types.length === 1) seen.set(types[0], otherTypes[0])
return otherTypes[0]
// if(functionTypes.length === 1) {
// // generate new function ?
// if(types.length === 1) seen.set(types[0], otherTypes[0])
// return otherTypes[0]
// } else {
// if(types.length === 1) seen.set(types[0], otherTypes[0])
// return otherTypes[0]
// }

const newType = cloneTypeWithoutAlias(otherTypes[0])
seen.set(otherTypes[0], newType)

return newType
} else if(otherTypes.length === 0 && objectTypes.length > 0) {
const newType = createAnonymousObjectType()
if(types.length === 1) seen.set(types[0], newType)
Expand Down Expand Up @@ -93,4 +116,21 @@ function _recursiveMergeIntersection(typeChecker: ts.TypeChecker, types: ts.Type

return symbol
}

function cloneTypeWithoutAlias(type: ts.Type) {
type = cloneClassInstance(type)

const symbol = type.getSymbol()

if(type.aliasSymbol && symbol) {
// remove type alias
type.aliasSymbol = undefined
}

return type
}
}

function cloneClassInstance<T>(orig: T): T {
return Object.assign(Object.create(Object.getPrototypeOf(orig)), orig) as T
}
2 changes: 2 additions & 0 deletions tests/cases/lambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type f = (arg1: string, arg2: boolean) => void
type t = { a: string } | f
2 changes: 2 additions & 0 deletions tests/lib/baselines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export async function acceptBaselines() {
}

export async function generateBaselineTests() {


const allTestCases = await getTestCases()

describe("baselines", () => {
Expand Down

0 comments on commit 293b3bb

Please sign in to comment.