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.

TypeScript Definition Help

See original GitHub issue

Hi all,

I’m working on a TypeScript definition for withStyle. What I have so far is:

type identity<T> = (arg: T) => T;

//TODO: This could be better, specific type information about the component is lost
export default function withStyles<P, S, T>(style: any): identity<new (...args: T[]) => React.Component<P, S>>

While this is OK, clearly some type information about the component is being lost, and I’m not too sure what type to use for the style object. Any ideas how to make this better?

Note that

export default function withStyles<T extends React.Component>(style: any): identity<T>

only works if you specify the return type (with withStyles<ComponentName>). Trying to avoid explicit generics if possible.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:13

github_iconTop GitHub Comments

6reactions
jknanda78commented, Feb 2, 2021

Hello @danielmhanover, are you still working on the typescript? I am expecting @types/isomorphic-style-loader. I tried with above suggestions but still seeing issues. Would you or anyone has a working solution?

Thanks in advance.

3reactions
ddsilvacommented, Aug 17, 2020

Following the thread, this is a sugestion for *.[s]css files, useStyles hook and StyledContext in global.d.ts file:

type Dispose = () => void
type InsertCssItem = () => Dispose
type GetCSSItem = () => string
type GetContent = () => string

interface Style {
  [key: string]: InsertCssItem | GetCSSItem | GetContent | string
  _insertCss: InsertCssItem
  _getCss: GetCSSItem
  _getContent: GetContent
}

declare module '*.scss' {
  const style: Style
  export default style
}

declare module '*.css' {
  const style: Style
  export default style
}

declare module 'isomorphic-style-loader/useStyles' {
  function useStyles(...styles: Style[]): void
  export default useStyles
}

declare module 'isomorphic-style-loader/StyleContext' {
  import { Context } from 'react'

  type RemoveGlobalCss = () => void
  type InsertCSS = (...styles: Style[]) => RemoveGlobalCss | void
  interface StyleContextValue {
    insertCss: InsertCSS
  }

  const StyleContext: Context<StyleContextValue>

  export { StyleContext as default, InsertCSS }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Type Declarations - TypeScript
TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like methods and ...
Read more >
Documentation - Introduction - TypeScript
The Declaration Files section is designed to teach you how to write a high-quality TypeScript Declaration File. We need to assume basic familiarity...
Read more >
Documentation - Declaration Reference - TypeScript
The purpose of this guide is to teach you how to write a high-quality definition file. This guide is structured by showing documentation...
Read more >
Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
Handbook - Basic Types - TypeScript
As in JavaScript, all numbers in TypeScript are either floating point values or BigIntegers. These floating point numbers get the type number ,...
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