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

Include type predicate information for functions #83

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions src/is-assignable/is-assignable-to-simple-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,17 @@ function isAssignableToSimpleTypeInternal(typeA: SimpleType, typeB: SimpleType,
}
}

// If typeA has a type predicate, make sure typeB's matches
if (typeA.typePredicate) {
if (!typeB.typePredicate) return false;
if (!isAssignableToSimpleTypeCached(typeA.typePredicate.type, typeB.typePredicate.type, options)) {
return false;
}
if (typeA.typePredicate.parameterIndex !== typeB.typePredicate.parameterIndex) {
return false;
}
}

// Test "this" types
const typeAThisParam = typeA.parameters.find(arg => arg.name === "this");
const typeBThisParam = typeB.parameters.find(arg => arg.name === "this");
Expand Down
8 changes: 8 additions & 0 deletions src/simple-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,26 @@ export interface SimpleTypeFunctionParameter {
readonly initializer: boolean;
}

export interface SimpleTypeTypePredicate {
readonly paramaterName: string;
readonly paramaterIndex: number;
readonly type: SimpleType;
}

export interface SimpleTypeFunction extends SimpleTypeBase {
readonly kind: "FUNCTION";
readonly parameters?: SimpleTypeFunctionParameter[];
readonly typeParameters?: SimpleTypeGenericParameter[];
readonly returnType?: SimpleType;
readonly typePredicate?: SimpleTypeTypePredicate;
}

export interface SimpleTypeMethod extends SimpleTypeBase {
readonly kind: "METHOD";
readonly parameters: SimpleTypeFunctionParameter[];
readonly typeParameters?: SimpleTypeGenericParameter[];
readonly returnType: SimpleType;
readonly typePredicate?: SimpleTypeTypePredicate;
}

// ##############################
Expand Down
10 changes: 9 additions & 1 deletion src/transform/to-simple-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,15 @@ function getSimpleFunctionFromSignatureDeclaration(

const typeParameters = getTypeParameters(signatureDeclaration, options);

return { name, kind, returnType, parameters, typeParameters } as SimpleTypeFunction | SimpleTypeMethod;
const tsTypePredicate = checker.getTypePredicateOfSignature(signature);
const typePredicateType = tsTypePredicate && toSimpleTypeCached(tsTypePredicate.type, options);
const typePredicate = typePredicateType && {
parameterName: tsTypePredicate.parameterName,
parameterIndex: tsTypePredicate.parameterIndex,
type: typePredicateType
};

return { name, kind, returnType, parameters, typeParameters, typePredicate } as SimpleTypeFunction | SimpleTypeMethod;
}

function getRealSymbolName(symbol: ESSymbol, ts: typeof tsModule): string | undefined {
Expand Down
9 changes: 8 additions & 1 deletion test/type-combinations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ export const FUNCTION_TYPES: TypescriptType[] = [
`((a: number, b?: string) => string)`,
`((a: number, b: string) => string)`,
`(): string | null`,
`(): string | boolean | null | {a: string}`
`(): string | boolean | null | {a: string}`,
`(a: unknown) => boolean`,
`(a: string|number) => a is string`,
`(b: string|number) => b is number`,
`(a: unknown) => a is string|number`
// TODO: the following line exercises type predicate parameterIndex compatability,
// but fails the test for assignability with one of FUNCTION_REST_TYPES.
// `(a: string|number, b: string|number) => b is number`
];

export const FUNCTION_THIS_TYPES: TypescriptType[] = [`(this: string, a: number) => any`, `(this: number, a: number) => any`, `(this: number) => any`];
Expand Down