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

Typecast for generic hashes not works as expected #29145

Open
erickskrauch opened this issue Dec 24, 2018 · 0 comments
Open

Typecast for generic hashes not works as expected #29145

erickskrauch opened this issue Dec 24, 2018 · 0 comments
Labels
Bug A bug in TypeScript Domain: Contextual Types The issue relates to contextual types
Milestone

Comments

@erickskrauch
Copy link

TypeScript Version: 3.2.2

Search Terms:

  • object generic

Code

type Scope = {
    $scan: () => void;
}

type HashItemAsFunction<S> = <S extends Scope>(element: HTMLElement, scope: S) => void;

type HashItemAsObject<S extends Scope> = {
    link?: HashItemAsFunction<S>;
};

type HashItem<S extends Scope> = HashItemAsFunction<S> | HashItemAsObject<S>;

interface HashDefinitionValues<S extends Scope> {
    [Identifier: string]: HashItem<S>;
}

type HashDefinition<S extends Scope = Scope> = {
    [key: string]: HashDefinitionValues<S>;
}

class Test {
    static hash: HashDefinition;
}

// Module code

type ModuleScope = Scope & {
    myKey: string;
}

(Test.hash as HashDefinition<ModuleScope>).subType1.child = {
    link: (element, scope) => {
        scope.myKey = '123';
        scope.$scan();
    },
};

(Test.hash.subType1 as HashDefinitionValues<ModuleScope>).child = {
    link: (element, scope) => {
        scope.myKey = '123';
        scope.$scan();
    },
};

(Test.hash.subType1.child as HashItem<ModuleScope>) = {
    link: (element, scope) => {
        scope.myKey = '123';
        scope.$scan();
    },
};

Expected behavior:

One of type casts should provide ModuleScope type to the scope variable, but it only provides base Scope in any case.

Actual behavior:

src/test.ts:33:15 - error TS2339: Property 'myKey' does not exist on type 'S'.

33         scope.myKey = '123';
                 ~~~~~

src/test.ts:40:15 - error TS2339: Property 'myKey' does not exist on type 'S'.

40         scope.myKey = '123';
                 ~~~~~

src/test.ts:47:15 - error TS2339: Property 'myKey' does not exist on type 'S'.

47         scope.myKey = '123';
                 ~~~~~

Playground Link:
https://goo.gl/4kQHBW

Related Issues:
Didn't find any.

@weswigham weswigham added Bug A bug in TypeScript Domain: Contextual Types The issue relates to contextual types labels Dec 25, 2018
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Contextual Types The issue relates to contextual types
Projects
None yet
Development

No branches or pull requests

3 participants