question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support TypeScript declaration merging

See original GitHub issue

Slate: 0.58.1

I’d really like to refine the internal editor types. Typescript supposedly has support for this:

https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation

But I think its not working currently, because for example text.d.ts exports a interface Text and a const Text so I think Typescript is having trouble with that.

Ideally, I can just write this code:

declare module "slate" {
	interface Text {
		bold?: string
	}
}

And now the text type everywhere in my codebase has a bold property.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
thesunnycommented, Jun 5, 2020

Declaration merging requires an interface; however, an interface doesn’t support union types.

Because of this, I wonder if we should close this issue in favour of a generic type. An example is a block that can be a paragraph, heading or list. With a type, we can define it as:

type MyElement = Element &
  (
    | { type: "paragraph" }
    | { type: "heading"; level: number }
    | { type: "list-item"; depth: number }
  )

The benefit is that we can use type discrimination:

if (element.type === 'heading') {
  const level = element.level // works!
  // the following would give a useful TypeScript error
  // const depth = element.depth
}
1reaction
ccorcoscommented, May 16, 2020

Yeah, I don’t like that pattern either (and don’t do it myself). I was just trying to come up with a small and pragmatic solution for you all.

On Wed, May 13, 2020 at 11:59 PM Oleksii Shurubura notifications@github.com wrote:

I can confirm that renaming interface Text → interface IText solves this problem.

The prefix I may be confusing and the whole idea of an interface of hiding implementation + not telling anyone that you are using an interface. The practice of having I in front of interfaces leads that you describe parameters, data structures with I which is wrong. It’s a data structure, not interface.

https://stackoverflow.com/questions/31876947/confused-about-the-interface-and-class-coding-guidelines-for-typescript

I, personally, like that slate does not use I prefix.

PS: Microsoft and Facebook do not use it for a reason.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ianstormtaylor/slate/issues/3680#issuecomment-628430772, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANWDXYPRH7RQQFZ6XNEL4LRROJGTANCNFSM4M7PTAIQ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Declaration Merging - TypeScript
For the purposes of this article, “declaration merging” means that the compiler merges two separate declarations declared with the same name into a...
Read more >
Declaration merging in TypeScript for regular devs - Merixstudio
In TypeScript, when two separate declarations with the same name are being merged into a single definition, it is called declaration merging. It ......
Read more >
TypeScript: Enhance Variable Types with Declaration Merging
TypeScript : Enhance Variable Types with Declaration Merging. This story is about an often applied technique by libraries that offer full TypeScript support....
Read more >
What is Declaration Merging in Typescript ? - GeeksforGeeks
In Typescript, the term “declaration merging” refers to the compiler combining two declarations with the same name into a single definition.
Read more >
How can I use Typescript's declaration merging with an ...
I'm currently using Typescript 3.0.3. This does what I want, but I don't understand why I can't do the same thing with declaration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found