Repository navigation
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
TypeScript Version: 3.0.1-insiders.20180726 (typescript playground version)
Search Terms:
generic, inference
Code
Note: noImplicitAny set to true
class BaseClass<P = any> {
public props: P;
}
function w<W extends BaseClass>(
ctr: (new () => W),
props: W['props']
) { }
interface MyClassProps {
myFunc(options: { foo: string }): void;
}
class MyClass extends BaseClass<MyClassProps> {}
// eplicit passing the generic
w<MyClass>(MyClass, {
myFunc(options) {
options.foo; // correctly infers prop
options.bar; // correctly errors
}
});
// inferring `myFunc` sig types based on `MyClass` argument
w(MyClass, {
myFunc(options) { // doesn't infer argument type
options.foo;
options.bar;
}
});Expected behavior:
Expected the argument of the function to be inferred correctly.
Actual behavior:
Doesn't infer the argument type or provide auto complete on the props of the w() function (does however type check)
Related Issues: Possibly related to #14829?
Reacted by hswhite33
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Activity
RyanCavanaugh commentedon Aug 13, 2018
Generic inference is a single-pass process; inferring a type for
optionshere would require a process of an arbitrary number of passes.#14829 wouldn't fix this on its own, but it'd be a prerequisite for deferring applying a type to the
optionsparameter to allow a contextual type fromTto be present.agubler commentedon Aug 13, 2018
Thanks for the response Ryan Cavanaugh (@RyanCavanaugh), glad that I’m not just doing something silly!
agubler commentedon Feb 20, 2019
Ryan Cavanaugh (@RyanCavanaugh) Interestingly using
tsxwith the following a simple globalJSXnamespacethe properties and any function arguments are all inferred correctly:
I guess this is because something "special" is happening?