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

[BUG] In the generated description file, the class type does not check the implemented interface. #26555

Closed
ZSkycat opened this issue Aug 20, 2018 · 3 comments
Assignees
Labels
Duplicate An existing issue was already created

Comments

@ZSkycat
Copy link

ZSkycat commented Aug 20, 2018

TypeScript Version: 3.1.0-dev.20180818

Search Terms: description file class type interface implements

Code

export type LibObjectType = 'Circle' | 'Square';

export interface LibObject {
    readonly type: LibObjectType;
    readonly id: number;
}

export class LibCircle implements LibObject {
    readonly type = 'Circle';
    readonly id: number = Date.now();
}

export class LibSquare implements LibObject {
    readonly type = 'Square';
    readonly id: number = Date.now();
}

Expected behavior:

// index.d.ts
export declare class LibCircle implements LibObject {
    readonly type: 'Circle';
    readonly id: number;
}

image

Actual behavior:

// index.d.ts
export declare class LibCircle implements LibObject {
    readonly type: string;
    readonly id: number;
}

image

Playground Link:
https://github.com/ZSkycat/issue-typescript-20180821-1

Related Issues:

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Aug 20, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.1 milestone Aug 20, 2018
@weswigham
Copy link
Member

weswigham commented Aug 20, 2018

Duplicate of #25990,

Two part fix is already up involving #26556 and #26313

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Aug 20, 2018
@ZSkycat
Copy link
Author

ZSkycat commented Aug 21, 2018

@RyanCavanaugh @weswigham
I found a new issue. The correct type cannot be inferred without using readonly.
Is this an expected behavior?
Should I open a new issue?

export type LibObjectType = 'Circle' | 'Square';

export interface LibObject {
    type: LibObjectType;
    id: number;
}

export class LibCircle implements LibObject {
    type = 'Circle';
    id: number = Date.now();
}

export class LibSquare implements LibObject {
    type = 'Square';
    id: number = Date.now();
}

tim 20180821103706

@weswigham
Copy link
Member

That's expected. Mutable locations widen literal types to their base unless appropriately contextually typed, and class properties aren't coming contextualized by their base. We have a bunch of discussions open on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants