-
Notifications
You must be signed in to change notification settings - Fork 12.8k
explicit return function type produces error when generic function result is returned directly #33394
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
Labels
Needs More Info
The issue still hasn't been fully clarified
Comments
It seems like this I don't use TS 3.6 yet but hopefully this implementation hasn't broken? Overloads seem to work well, type IfKey<T, K> = [K] extends [keyof T] ? Exclude<Required<T[K]>, undefined> : T
function deepPick<
T0 extends object
>(value:T0, path:[]): T0;
function deepPick<
T0 extends object,
K1 extends keyof T0,
>(value:T0, path:[K1]): IfKey<T0,K1>;
function deepPick<
T0 extends object,
K1 extends keyof T0,
K2 extends keyof IfKey<T0,K1>
>(value:T0, path:[K1,K2]): IfKey<IfKey<T0,K1>,K2>;
function deepPick(value:any, path:[string?, string?]): any {
return path.reduce((value: any, key: any) => {
return value && value[key];
}, value);
}
type User = {nickname:string;fullName:string;age?:number}
const users: {john: User;doe:User} = {
john: {
nickname: 'jo5',
fullName: 'Johny Five',
age: 123,
},
doe: {
nickname: 'doe',
fullName: 'John Doe',
age: Infinity
}
}
function getSomedata(): number {
return deepPick(users,['john','age']) || 0
}
function getSomedataNoErrorAssign(): number {
const result = deepPick(users,['john','age']) || 0
return result
}
function getSomedataNoError() {
return deepPick(users,['john','age']) || 0
} |
Is there a simpler reproduction of the difference here? |
So no difference @RyanCavanaugh both implementations are producing same error in 3.6 👀 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 3.6.3
Search Terms:
Code
Expected behavior:
Explicitly providing return type should work
Actual behavior:
When explicitly providing return type, TSC will error out on
deepPick
usage.In TS 3.5 there are 2 errors:
getSomedata
getSomedataNoErrorAssign
TS 3.5 vs TS 3.6
Playground Link: Demo - NOTE that playground is using old ts 3.5.1
Related Issues: #32937, #33002
The text was updated successfully, but these errors were encountered: