-
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
ts4.6.1-rc regression: Property ... does not exist on type 'Partial<MyStorage>[K]' #47890
Comments
Cleaned up: interface MyObj {
someKey: {
name: string;
}
someOtherKey: {
name: number;
}
}
const ref: MyObj = {
someKey: { name: "" },
someOtherKey: { name: 42 }
}
function func<K extends keyof MyObj>(k: K): MyObj[K]['name'] | undefined {
const myObj: Partial<MyObj>[K] = ref[k];
if (myObj) {
return myObj.name;
}
const myObj2: Partial<MyObj>[keyof MyObj] = ref[k];
if (myObj2) {
return myObj2.name;
}
return undefined;
} This appears to be caused by #47109, specifically that we no longer immediately resolve I believe this can be worked around with some minor modification (shown the repro as |
@ahejlsberg ping to confirm this is an intended break |
This duplicates the type information from const __storage: Partial<Grid.Storage> = {...}
// inside function
const storageForKey:Partial<Grid.Storage>[keyof Grid.Storage] = __storage[key];
// does not work:
const storageForKey:Partial<Grid.Storage>[K] = __storage[key]; But fine enough for me. Thanks for your work. Complete examplenamespace Grid {
export interface Storage {
columnWidths: StorageItem<number[]>;
}
export interface StorageItem<V> {
/** value for anon */
anonymousUser: V;
/** value for each user */
users: Record<string, V>;
}
}
const __storage: Partial<Grid.Storage> = {
columnWidths: {
anonymousUser: [1],
users: {
admin: [2]
}
}
};
function __getStoredValue<K extends keyof Grid.Storage>(
key: K
): Grid.Storage[K]['anonymousUser'] | undefined {
let user = 'admin';
const storageForKey = __storage[key];
//const storageForKey:Partial<Grid.Storage>[K] = __storage[key]; // does not work
//const storageForKey:Partial<Grid.Storage>[keyof Grid.Storage] = __storage[key]; // works
if (!storageForKey) {
return undefined;
}
if (!user) {
return storageForKey.anonymousUser; // <--
} else {
if (storageForKey.users) { // <--
return storageForKey.users[user]; // <--
}
return undefined;
}
}
const result = __getStoredValue('columnWidths');
console.log(result); |
This is not an intended break. This change in #47109 was part of an early evolution of the PR, but should have been removed. It's an easy fix, I'll put up a PR. |
Successfully tested with TS |
Bug Report
π Search Terms
partial, generics
π Version & Regression Information
Tested with nuget package 4.6.0 and 4.6.1-rc and "nightly" on playground with is labeled v4.6.0-dev.20220116
β― Playground Link
Workbench Repro
π Actual behavior
TS4.5.4 is happy to compile my code, but ts4.6.x complains on access
anonymousUser
orusers
:Property 'anonymousUser' does not exist on type 'Partial[K]'.
Property 'users' does not exist on type 'Partial[K]'.
Property 'users' does not exist on type 'Partial[K]'.
π Expected behavior
Should still compile as the property exists on the type.
The text was updated successfully, but these errors were encountered: