Skip to content
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

Closed
HolgerJeromin opened this issue Feb 14, 2022 · 5 comments Β· Fixed by #47953
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@HolgerJeromin
Copy link
Contributor

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

  • This changed between versions 4.5.4 and 4.6.0

⏯ Playground Link

function __getStoredValue<K extends keyof Grid.Storage>(
    key: K
): Grid.Storage[K]['anonymousUser'] | undefined {
    let user = 'admin';

    let storageForKey = {
        anonymousUser: [1],
        users: {
            admin: [2]
        }
    } as Partial<Grid.Storage>[K];

    if (!storageForKey) {
        return undefined;
    }

    if (!user) {
        return storageForKey.anonymousUser;  // <--
    } else {
        if (storageForKey.users) {  // <--
            return storageForKey.users[user]; // <--
        }
        return undefined;
    }
}
namespace 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 result = __getStoredValue('columnWidths');
console.log(result);

Workbench Repro

πŸ™ Actual behavior

TS4.5.4 is happy to compile my code, but ts4.6.x complains on access anonymousUser or users:

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.

@RyanCavanaugh
Copy link
Member

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 Partial<MyObj>[K] to a non-generic type.

I believe this can be worked around with some minor modification (shown the repro as myObj2) which wouldn't cause any loss of correctness in working code, but we should document it in the blog regardless.

@RyanCavanaugh
Copy link
Member

@ahejlsberg ping to confirm this is an intended break

@RyanCavanaugh RyanCavanaugh added the Breaking Change Would introduce errors in existing code label Feb 17, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.6.1 milestone Feb 17, 2022
@HolgerJeromin
Copy link
Contributor Author

This duplicates the type information from __storage object into the function variable.

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 example
namespace 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);

@ahejlsberg
Copy link
Member

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.

@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Breaking Change Would introduce errors in existing code labels Feb 18, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Feb 18, 2022
@HolgerJeromin
Copy link
Contributor Author

Successfully tested with TS 4.7.0-dev.20220220. Thanks for your quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants