Skip to content

Declarations and "Exported variable 'XXX' has or is using name 'YYY' from external module "ZZZ" but cannot be named." #24666

Closed
@jamiewinder

Description

@jamiewinder

I was pleased to see #9944 had been closed with what looked like a feature of 2.9 'fixing' the issue. However, it still doesn't seem to be working as I'd expect. Take this example:

import styled from "styled-components";

export const Container = styled.div`
    background: red;
`;

With a normal TypeScript project, using the tsconfig default except enabling declarations. I get three errors:

src/styles.ts(3,14): error TS4023: Exported variable 'Container' has or is using name 'React.ClassAttributes' from external module "/node_modules/@types/react/index" but cannot be named.
src/styles.ts(3,14): error TS4023: Exported variable 'Container' has or is using name 'React.HTMLAttributes' from external module "/node_modules/@types/react/index" but cannot be named.
src/styles.ts(3,14): error TS4023: Exported variable 'Container' has or is using name 'StyledComponentClass' from external module "/node_modules/styled-components/typings/styled-components" but cannot be named.

The obvious solution seems to be importing the types involved:

import styled, { StyledComponentClass } from "styled-components";
import * as React from "react";

export const Container = styled.div`
    background: red;
`;

Which would be fine, I suppose (though I don't see why it is necessary..?) but this doesn't work with noUnusedLocals as you get more errors:

src/styles.ts(1,18): error TS6133: 'StyledComponentClass' is declared but its value is never read.
src/styles.ts(2,13): error TS6133: 'React' is declared but its value is never read.

... However you can work around that by explicitly using the types, but even with this basic example it gets absurdly verbose:

import styled, { StyledComponentClass } from "styled-components";
import * as React from 'react';

export const Container: StyledComponentClass<React.ClassAttributes<HTMLDivElement> & React.HTMLAttributes<HTMLDivElement>, any, React.ClassAttributes<HTMLDivElement> & React.HTMLAttributes<HTMLDivElement>> = styled.div`
    background: red;
`;

What is the recommended solution / workaround to this?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions