-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type inference fails #41712
Comments
Tried shrinking it a bit, wasn't too successful (new playground link). But I replaced the domain-specific language with some generic terms so I hope it is a bit easier to understand. I'll explain it in more detail below. class Wrapper<T = any> {
public value?: T;
}
type WrappedMap = Record<string, Wrapper>;
type Unwrap<D extends WrappedMap> = {
[K in keyof D]: D[K] extends Wrapper<infer T> ? T : never;
};
type MappingComponent<I extends WrappedMap, O extends WrappedMap> = {
setup(): { inputs: I; outputs: O };
map?: (inputs: Unwrap<I>) => Unwrap<O>;
};
declare function createMappingComponent<I extends WrappedMap, O extends WrappedMap>(def: MappingComponent<I, O>): void;
createMappingComponent({
setup() {
return {
inputs: {
num: new Wrapper<number>(),
str: new Wrapper<string>()
},
outputs: {
bool: new Wrapper<boolean>(),
str: new Wrapper<string>()
}
};
},
map(inputs) {
return {
bool: inputs.nonexistent, // (1)
str: inputs.num, // (2)
};
}
}); Essentially, the code is about creating a The Similar to above, I expect
Interestingly, the outputs |
I don't see any evidence of a bug here. The inference for a setup like this is extremely complex, but if the type parameters are set up correctly then you would see the expected errors. class Wrapper<T> {
public value?: T;
}
type WrappedMap<Values> = Record<string, Wrapper<Values>>;
type Unwrap<D extends WrappedMap<unknown>> = {
[K in keyof D]: D[K] extends Wrapper<infer T> ? T : never;
};
type MappingComponent<I extends WrappedMap<unknown>, O extends WrappedMap<unknown>> = {
setup(): { inputs: I; outputs: O };
map?: (inputs: Unwrap<I>) => Unwrap<O>;
};
declare function createMappingComponent<I extends WrappedMap<unknown>, O extends WrappedMap<unknown>>(def: MappingComponent<I, O>): MappingComponent<I, O>;
const res = createMappingComponent({
setup() {
return {
inputs: {
num: new Wrapper<number>(),
str: new Wrapper<string>()
},
outputs: {
bool: new Wrapper<boolean>(),
str: new Wrapper<string>()
}
};
},
// Errors as expected
map(inputs) {
return {
bool: inputs.nonexistent, // (1)
str: inputs.num, // (2)
};
}
}); Because there's a |
I tried your code, however, I get errors on correct code now, because the values of map(inputs) {
return {
bool: true,
str: inputs.str,
};
} Results in this error:
This may very well be a design limitation, because when the const res: MappingComponent<{
num: Wrapper<number>;
str: Wrapper<string>;
}, {
bool: Wrapper<boolean>;
str: Wrapper<string>;
}> As soon as I write the mapping function, the type of const res: MappingComponent<Record<string, Wrapper<unknown>>, Record<string, Wrapper<unknown>>> |
TypeScript Version: 4.2.0-dev.20201127
Search Terms:
Code
Expected behavior:
(1)
should give an error, sinceinputs.v
does not exist(2)
should give an error, sinceinputs.a
is anumber
, whiled
is astring
Actual behavior:
The code above compiles without errors. Also, Intellisense is not working at the
calculate
function insidedefineNode
.Playground Link:
Link
Related Issues:
The text was updated successfully, but these errors were encountered: