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

Fix type of PO.Item #38

Merged
merged 1 commit into from
Apr 21, 2020
Merged

Fix type of PO.Item #38

merged 1 commit into from
Apr 21, 2020

Conversation

kirillku
Copy link
Contributor

@kirillku kirillku commented Apr 21, 2020

Currently, it's not possible to access Item type. The proposed change should fix the issue.

declare class Item {
    public msgid: string;
    public msgctxt?: string;
    public references: string[];
    public msgid_plural?: string;
    public msgstr: string[];
    public comments: string[];
    public extractedComments: string[];
    public flags: Record<string, boolean | undefined>;
    private nplurals: number;
    private obsolete: boolean;

    public toString(): string;
}

// This is how it's done currently
declare class CurrentPO { 
    public static Item: typeof Item; 
}

// Any of those give warnings or provide a wrong type
declare const currentItem1: CurrentPO.Item
declare const currentItem2: typeof CurrentPO.Item
currentItem2.msgid
declare const currentItem3: InstanceType<CurrentPO.Item>

// This way it will be possible to get Item type without any ts warnings
declare class UpdatedPO { 
    public static Item: Item; 
}

// Works
declare const updatedItem: typeof UpdatedPO.Item
updatedItem.msgid

ts playground

Related to #36

@septs
Copy link
Contributor

septs commented Apr 21, 2020

// works
const item = new CurrentPO.Item()

// not works
// This expression is not constructable.
//  Type 'Item' has no construct signatures.(2351)
const updatedItem = new UpdatedPO.Item();

@rubenv rubenv merged commit fe23027 into rubenv:master Apr 21, 2020
@septs
Copy link
Contributor

septs commented Apr 21, 2020

@rubenv The PR, Will affect to new PO.Item()

@rubenv
Copy link
Owner

rubenv commented Apr 21, 2020

Oh wait I read this wrong. Reverted the merge!

@rubenv
Copy link
Owner

rubenv commented Apr 21, 2020

So TS definitions are not my area of expertise. Just let me know when it's actually OK and I'll do the right thing :-)

@kirillku
Copy link
Contributor Author

@rubenv what do you think about adding named imports then?

@septs
Copy link
Contributor

septs commented Apr 21, 2020

@kirillku switch to esmodule is the best choice

@septs
Copy link
Contributor

septs commented Apr 21, 2020

OR using namespace

declare namespace PO {
    export type ItemType = Item
}
const item: PO.ItemType = new PO.Item()

This is the only solution currently available

CC @kirillku

@kirillku kirillku deleted the patch-1 branch May 3, 2020 14:31
@kirillku
Copy link
Contributor Author

kirillku commented May 3, 2020

@septs I found a way how to to it without making any changes to lib types. Looks quite hacky through.

type ItemType = InstanceType<typeof PO.Item>

ts playground

@septs
Copy link
Contributor

septs commented May 3, 2020

@kirillku bingo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants