You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionfn<Textends{[PinK]: string},KextendskeyofT>(obj: T,key: K){constval: string=obj[key];// error TS2322: Type 'T[K]' is not assignable to type 'string'}fn({str: "s",num: 1},"str");// OKfn({str: "s",num: 1},"num");// error TS2345: Argument of type '{ str: string; num: number; }' is not assignable to parameter of type '{ num: string; }
Expected behavior:
Given the constraints, I would expect obj[key] in this case to be of type string.
Actual behavior:
The type of obj[key] is the less specific T[K].
Considering the error at the second call site (which is expected), it seems that the type checking done there is complete and working. However, that same type info is not carried through into the function body itself.
The text was updated successfully, but these errors were encountered:
I witnessed the same bug. I noticed that this problem only exists if both obj and key are TypeParameters.
// typescript@2.7.0-rc with --strictNullChecksdeclarefunctionfoo(p: string): void;interfaceI{foo?: string;bar?: string;}functionworking1<TextendsI>(o: T,k: keyofI){foo(o[k]);// getting expected type error here, because type is `string | undefined`}functionworking2<KextendskeyofI>(o: I,k: K){foo(o[k]);// getting expected type error here, because type is `string | undefined`}functionnotWorking<TextendsI,KextendskeyofT>(o: T,k: K){foo(o[k]);// expected type error, but got none}
Inside notWorkingo[k] is treated as any. At least that's what checker.getApparentType(checker.getTypeAtLocation(node)) tells me.
TypeScript Version: 2.1.4 / nightly (2.2.0-dev.20161216)
Expected behavior:
Given the constraints, I would expect
obj[key]
in this case to be of typestring
.Actual behavior:
The type of
obj[key]
is the less specificT[K]
.Considering the error at the second call site (which is expected), it seems that the type checking done there is complete and working. However, that same type info is not carried through into the function body itself.
The text was updated successfully, but these errors were encountered: