Given #16072, we can now make more powerful inferences given contextual types on the return type of a call.
However, given the following:
function compose<A, B, C>(
f: (x: A) => B,
g: (y: B) => C,
): (x: A) => C;
compose(
x => [x],
y => ({ foo: x })
)
A = {}, B = {}, C = {}.@mhegazy may have notes on weak types (#16047) and synthesized namespace records (#16093)
A Weak type is a type with only optional members, no index no call no construct and no props
default import interopusers today need a loader or a transpiler to make things work
import {x, y, z} from "foo" -- do nothing here, current behaviour is correctimport * as ns from "foo" -- add a new helper __importStarimport d from "foo' -- write `obj && obj.__esModule ? obj : {default: obj}{x, y, z} does not change* as ns does not changedefault import behaves like --allowSynthaticDefaults5/26/2017, probably?
yup. fixed.
I've laid out my thoughts on how #9366 might be solved and some of the experience I've acquired while trying to make it work.
I made several attempts to tackle this, however unfortunately all of them had considerable drawbacks. Recently I came up with a new scheme, that I haven't tried implementing yet, but has some potential to actually work.
The only languages that I know that support proper polymorphic unification are all ML based. They use unification solvers, that's why I tried to use one as well. The big hurdles that hit me were:
extends)step 1: constraint generation - walking the AST and generating (type variable, type) pairs
type InferenceContext = {
constraints: [TypeVariable, Type][],
deferredConstraints: [Type, Type][],
typeParameters: Map<Type, boolean>, // only original signatures type parameters
polyVars: Map<Type, boolean>, // the original typeParamaters & any argument
// type parameters we encounter
}
step 2: solve the constraints
type UnificationCtx = {
types: Type[], // accumulate constituents here
firstMet: Type, // the first met polymorphic variable that is not one of the
// original type parameters
bounds: Type[] // type bounds encountered while unifying
// (might not be needed if we revise `checkApplicableSignature`)
}
UnificationCtx dictionary (merge if necessary)getInferredType Arbitrary polymorphic unification seems to preclude a unification solver. Another solution would be truly novel.
Unification can be done in an ad-hoc fashion, however the separation of the process into constraint generation and subsequent solving adds a lot of freedom and simplicity from a logical perspective.
PS: I'll try to play around with the above idea during the weekend. If something happens I'll put up a PR.
EDIT: Comment moved to the appropriate issue: https://github.com/Microsoft/TypeScript/issues/16093#issuecomment-304578015
I have a basic implementation of the proposed logic from https://github.com/Microsoft/TypeScript/issues/16114#issuecomment-304415315. I've shared my current progress and comments in https://github.com/Microsoft/TypeScript/issues/9949#issuecomment-306620505.
@gcnew get yourself hired at TS@MS, stop wasting everyone's time! :)
Most helpful comment
TL;DR
I've laid out my thoughts on how #9366 might be solved and some of the experience I've acquired while trying to make it work.
#9366
I made several attempts to tackle this, however unfortunately all of them had considerable drawbacks. Recently I came up with a new scheme, that I haven't tried implementing yet, but has some potential to actually work.
The only languages that I know that support proper polymorphic unification are all ML based. They use unification solvers, that's why I tried to use one as well. The big hurdles that hit me were:
extends)Sketches of the new idea:
step 1: constraint generation - walking the AST and generating (type variable, type) pairs
step 2: solve the constraints
UnificationCtxdictionary (merge if necessary)getInferredTypeHow are the outlined problems solved?
Is this the only approach?
Arbitrary polymorphic unification seems to preclude a unification solver. Another solution would be truly novel.
On unification
Unification can be done in an ad-hoc fashion, however the separation of the process into constraint generation and subsequent solving adds a lot of freedom and simplicity from a logical perspective.
Ideas that I've tried but have lead to a dead end:
PS: I'll try to play around with the above idea during the weekend. If something happens I'll put up a PR.