diff --git a/index.d.ts b/index.d.ts index 577d835..4767b58 100644 --- a/index.d.ts +++ b/index.d.ts @@ -342,6 +342,8 @@ export function prodType(typeParam: any): any; export function recomputeTypeVars(types: any, skip: number): any; +export function recomputeTypeVarsWithInverse(types: any, skip: number): [any, Map]; + export function setType(typeParam: any): any; export function structType(fields: any, rootSelf: any): any; diff --git a/lib/generics/generics.js b/lib/generics/generics.js index 0f984e3..71ef01a 100644 --- a/lib/generics/generics.js +++ b/lib/generics/generics.js @@ -93,16 +93,22 @@ export const assignFn = (funType, paramType, skip=0) => { // Ensures that no type variables overlap export const recomputeTypeVars = (types, skip=0) => { + return recomputeTypeVarsWithInverse(types, skip)[0]; +}; + +export const recomputeTypeVarsWithInverse = (types, skip=0) => { let nextIdx = skip; - return types.map(type => { + const inverse = new Map(); + return [types.map(type => { const substitutions = new Map(); const typeVars = occurring(type); for (const typeVar of typeVars) { const idx = nextIdx++; if (typeVar !== UNBOUND_SYMBOLS[idx]) { substitutions.set(typeVar, TYPE_VARS[idx]); + inverse.set(UNBOUND_SYMBOLS[idx], typeVar); } } return substitute(type, substitutions); - }); -}; + }), inverse]; +} \ No newline at end of file