Closed mak438 closed 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
numberType!==true
stringType!==true
arrayOrTupleType===undefined || arrayOrTupleType.kind==tuple
(and all the tuple types are lower bounds)objectType===undefined
or all the tuple types in the dictionary are lower bounds.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)?
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"}}
.
TypeDeducer
should get the lower bounds on all the types it sees from examples.