cucapra / js-to-ts-synthesis

guess types for JavaScript libraries by running the tests
0 stars 0 forks source link

Get lower bounds on types #7

Closed mak438 closed 7 years ago

mak438 commented 7 years ago

TypeDeducer should get the lower bounds on all the types it sees from examples.

mak438 commented 7 years ago

What do you think about the following type representation? I realize this isn't the full Typescript, but it might be a useful subset.

export interface AnyType {
    kind: "any"
}

export interface RestrictedType {
    kind: "restricted"
    nullType?: true;
    undefinedType?: true;
    booleanType?: true | Set<boolean>;
    numberType?: true | Set<number>;
    stringType?: true | Set<string>;
    functionType?: true;
    arrayOrTupleType?: {kind: "array", type: Type} | {kind: "tuple", type: Type[]};
    objectType?: true | Dictionary<string, Type> // If an object can have any keys, it must be an any type (see https://www.typescriptlang.org/docs/handbook/basic-types.html)
}

export type Type = AnyType | RestrictedType;

Lower bounds will always be a RestrictedType with

sampsyo commented 7 years ago

OK, this makes sense! Is the plan to use a similar representation for upper bounds, where some aspects would probably need to be negative (i.e., some types excluded)?

mak438 commented 7 years ago

It shouldn't be too difficult to extend that to something like "anything except 42" by flagging each set as exclude or include. Restricting all strings, for example, can be done by setting everything else to true, and arrayOrTupleType to {kind: "array", type: {kind: "any"}}.