-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
TS 3.4 Type Inference Regression #30778
Comments
The issue here is that in 3.4 you now have two competing sources of inference for Possible fixes:
createRouter({
test1,
test2: defineRoute({
hello: "path.param.number"
} as const, p => `/${p.hello}`) // <-- 'as const'
});
type RouteDefinitionDataCollection = {
[routeName: string]: RouteDefinitionData<ParameterDefinitionCollection>;
// was 'any'
};
|
Thanks Ryan! The second one is definitely the route I'd like to go down. Unfortunately, it looks like that fix doesn't work in strict mode (specifically (looks like the playground doesn't persist options selections to the url so you'll have to manually set "strictFunctionTypes" to true) Summary of what I saw:
Any suggestions here? I'd like to get this working no matter the compiler settings someone has set. I really appreciate your help! |
TypeScript Version: 3.4.1
Search Terms: type inference
Code
Expected behavior:
No errors and properly inferring
p.hello
to be of typestring
.Actual behavior:
For
test2
(but interestingly nottest1
) there is this error: "Property 'hello' does not exist on type 'Record<never, string>'."Playground Link: https://www.typescriptlang.org/play/#src=type%20QueryParamString%20%3D%20%22query.param.string%22%3B%0D%0Atype%20QueryParamNumber%20%3D%20%22query.param.number%22%3B%0D%0Atype%20PathParamString%20%3D%20%22path.param.string%22%3B%0D%0Atype%20PathParamNumber%20%3D%20%22path.param.number%22%3B%0D%0Atype%20QueryParamStringOptional%20%3D%20%22query.param.string.optional%22%3B%0D%0Atype%20QueryParamNumberOptional%20%3D%20%22query.param.number.optional%22%3B%0D%0A%0D%0Atype%20KeysMatching%3CT%2C%20V%3E%20%3D%20%7B%0D%0A%20%20%5BK%20in%20keyof%20T%5D%3A%20T%5BK%5D%20extends%20V%20%3F%20K%20%3A%20never%0D%0A%7D%5Bkeyof%20T%5D%3B%0D%0A%0D%0Atype%20ParameterDefinition%20%3D%0D%0A%20%20%7C%20QueryParamString%0D%0A%20%20%7C%20QueryParamNumber%0D%0A%20%20%7C%20PathParamString%0D%0A%20%20%7C%20PathParamNumber%0D%0A%20%20%7C%20QueryParamStringOptional%0D%0A%20%20%7C%20QueryParamNumberOptional%3B%0D%0A%0D%0Atype%20ParameterDefinitionCollection%20%3D%20%7B%0D%0A%20%20%5BparameterName%3A%20string%5D%3A%20ParameterDefinition%3B%0D%0A%7D%3B%0D%0A%0D%0Atype%20PathFn%3CT%20extends%20ParameterDefinitionCollection%3E%20%3D%20(%0D%0A%20%20params%3A%20Record%3CKeysMatching%3CT%2C%20PathParamNumber%20%7C%20PathParamString%3E%2C%20string%3E%0D%0A)%20%3D%3E%20string%3B%0D%0A%0D%0Atype%20RouteDefinitionData%3CT%20extends%20ParameterDefinitionCollection%3E%20%3D%20%7B%0D%0A%20%20params%3A%20T%3B%0D%0A%20%20path%3A%20PathFn%3CT%3E%3B%0D%0A%7D%3B%0D%0A%0D%0Atype%20RouteDefinitionDataCollection%20%3D%20%7B%0D%0A%20%20%5BrouteName%3A%20string%5D%3A%20RouteDefinitionData%3Cany%3E%3B%0D%0A%7D%3B%0D%0A%0D%0Adeclare%20function%20defineRoute%3CT%20extends%20ParameterDefinitionCollection%3E(%0D%0A%20%20params%3A%20T%2C%0D%0A%20%20path%3A%20PathFn%3CT%3E%0D%0A)%3A%20RouteDefinitionData%3CT%3E%3B%0D%0A%0D%0Adeclare%20function%20createRouter%3CT%20extends%20RouteDefinitionDataCollection%3E(%0D%0A%20%20routeDefinitions%3A%20T%0D%0A)%3A%20any%3B%0D%0A%0D%0Aconst%20test1%20%3D%20defineRoute(%7B%0D%0A%20%20hello%3A%20%22path.param.number%22%0D%0A%7D%2C%20p%20%3D%3E%20%60%2F%24%7Bp.hello%7D%60)%3B%0D%0A%0D%0AcreateRouter(%7B%0D%0A%20%20%20%20test1%2C%0D%0A%20%20%20%20test2%3A%20defineRoute(%7B%0D%0A%20%20%20%20%20%20%20%20hello%3A%20%22path.param.number%22%0D%0A%20%20%20%20%7D%2C%20p%20%3D%3E%20%60%2F%24%7Bp.hello%7D%60)%0D%0A%7D)%3B%0D%0A
Related Issues: Maybe #30074
I honestly don't know the compiler well enough to know if this is a duplicate or not. The manifestation of this issue is certainly different than in some of the other 3.4 regression issues.
I tried to reduce noise as much as possible in the above example while still preserving context of what is actually trying to be accomplished. The above code works as expected in
3.3.4000
but not in3.4.1
.The text was updated successfully, but these errors were encountered: