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 type annotation lost on dynamically named variable in custom icon template

See original GitHub issue

🐛 Bug Report

Custom templates don’t seem to preserve : type annotations on variables.

Given this custom template:

module.exports = function iconTemplate({ componentName }, { tpl }) {
  return tpl`
      export const test: string = '';
      export const ${componentName}: string = '';
    `;
};

The output code .tsx file from svgr --typescript on an icon.svg file is like:

export const test: string = "";
export const SvgIcon = "";

Note the lack of type annotation on SvgIcon.

Edit: oh, and you can work around this with an as cast at the end.

export const ${componentName} = '' as string; // (or whatever type you wanted)

To Reproduce

git clone https://github.com/JoshuaKGoldberg/repros
cd repros
git checkout svgr-type-annotation
npm i
npm run build
cat built/Icon.tsx

Expected behavior

It should look like:

export const test: string = "";
export const SvgIcon: string = "";

Link to repl or repo (highly encouraged)

https://github.com/JoshuaKGoldberg/repros/tree/svgr-type-annotation

This case may seem trivial but it’s a reduction from trying to get an explicit type annotation on a React.forwardRef component. TypeScript’s .d.ts emit goes from React.forwardRef<SVGSVGElement, GamutIconProps> to React.ForwardRefExoticComponent<Pick<GamutIconProps, "string" | "flexBasis" | ... (dozens of other properties) ...> & React.RefAttributes<SVGSVGElement>>. https://github.com/Codecademy/gamut/pull/2211/commits/01c1bfb1d0adcde727f76284305a810760ea5855

Run npx envinfo --system --binaries --npmPackages @svgr/core,@svgr/cli,@svgr/webpack,@svgr/rollup --markdown --clipboard

Paste the results here:

## System:
 - OS: macOS 11.6
 - CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
 - Memory: 592.55 MB / 16.00 GB
 - Shell: 5.8 - /bin/zsh
## Binaries:
 - Node: 14.17.2 - ~/.nvm/versions/node/v14.17.2/bin/node
 - Yarn: 1.22.17 - /usr/local/bin/yarn
 - npm: 8.1.0 - ~/.nvm/versions/node/v14.17.2/bin/npm
 - Watchman: 2021.10.18.00 - /usr/local/bin/watchman
## npmPackages:
 - @svgr/cli: ^6.1.2 => 6.1.2 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LoicUVcommented, Feb 2, 2022

Looks like it doesn’t work when adding type in combination with ${componentName}.
works but not practical:

const SomeName: ForwardRefExoticComponent<IconComponentProps & RefAttributes<HTMLSpanElement>> = (props, ref) => (

strips types:

const ${componentName}: ForwardRefExoticComponent<IconComponentProps & RefAttributes<HTMLSpanElement>> = (props, ref) => (

Works with this syntax @jonybekov:

const ${`${componentName}: ForwardRefExoticComponent<IconComponentProps & RefAttributes<HTMLSpanElement>>`} = (props, ref) => (
1reaction
jonybekovcommented, Jan 15, 2022

Casting might work for simple variables. But it’s not working with React.forwaredRef

Read more comments on GitHub >

github_iconTop Results From Across the Web

The starting point for learning TypeScript
Learn how to write declaration files to describe existing JavaScript. Important for DefinitelyTyped contributions. Introduction · Declaration Reference ...
Read more >
Type definition for dynamic, infinite, numbered variable names ...
I am looking for a solution, that defines 'x' + number as a valid attribute, but not 'y' + number while keeping existing...
Read more >
Announcing TypeScript 4.7 - Microsoft Developer Blogs
Today we're excited to announce the availability of TypeScript 4.7! If you're not yet familiar with TypeScript, it's a language that builds ...
Read more >
Semantic Highlight Guide | Visual Studio Code Extension API
A guide to syntax highlighting.
Read more >
TypeScript | IntelliJ IDEA Documentation - JetBrains
icon. TypeScript-aware coding assistance includes completion for keywords, labels, variables, parameters, and functions, error and syntax ...
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