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

string "ddd" not assignable to type '"ddd" | "dddd"' #32355

Closed
selfisekai opened this issue Jul 11, 2019 · 4 comments
Closed

string "ddd" not assignable to type '"ddd" | "dddd"' #32355

selfisekai opened this issue Jul 11, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@selfisekai
Copy link

TypeScript Version: 3.6.0-dev.20190711

Search Terms: string not assignable to type string

Code

export class BaseChannel {
  type: 'text' | 'voice' | 'direct';
}

export class TextChannel extends BaseChannel {
  type = 'text';
}

Expected behavior: assign successfully

Actual behavior: TS2416 error: Property 'type' in type 'TextChannel' is not assignable to the same property in base type 'BaseChannel'. Type 'string' is not assignable to type '"text" | "voice" | "direct"'.

Playground Link: ⛓️

Related Issues: did not find any

@orta
Copy link
Contributor

orta commented Jul 11, 2019

This is an interesting case of the widening of 'text' into string inside a mutable location.

While this is being discussed, this.type inside the constructor works:

export class BaseChannel {
  type: 'text' | 'voice' | 'direct';
}

export class TextChannel extends BaseChannel {
  // type = 'text';

  constructor() {
    super()
    this.type = 'text'
  }
}

playground link

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Jul 11, 2019

Or,

export class BaseChannel {
  type: 'text' | 'voice' | 'direct';
}

export class TextChannel extends BaseChannel {
  type = 'text' as const;
}

Playground

@DanielRosenwasser
Copy link
Member

See #32082 (also #340, #1373, #5749, #6118, #10570, #16944, #23911).

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jul 11, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

5 participants