GithubHelp home page GithubHelp logo

Comments (9)

pelotom avatar pelotom commented on August 16, 2024 2

FYI I've opened a PR on @types/react to use csstype as-is with a string index fallback: DefinitelyTyped/DefinitelyTyped#24688

from csstype.

frenic avatar frenic commented on August 16, 2024 2

Thank you! I'll do my best with SVG properties as soon as possible.

from csstype.

pelotom avatar pelotom commented on August 16, 2024 1

I’m as excited to apply conditional types to every problem as the next person, but I think this may be a case of diminishing returns on investment πŸ™‚ I would guess that 95% of downstream libraries don’t care to distinguish between the properties that are valid for one type of element vs another... the most pressing need is for a complete yet finite union of possible properties and their corresponding value types.

from csstype.

frenic avatar frenic commented on August 16, 2024

Nice catch, I'll fix that. I guess keywords containing only digits should be converted to a numeric literal instead.

SVG properties are missing in https://github.com/mdn/data. Here's an issue about that.

from csstype.

frenic avatar frenic commented on August 16, 2024

Here's an index of SVG properties. But there's no raw data to use as far as I can see. Either way, there's a complexity described here with SVG properties that some of them only apply to a very limited group of elements. Some of them only apply to one single element like the stop-color property.

from csstype.

frenic avatar frenic commented on August 16, 2024

I just did a quick test and it seems like React.CSSProperties are now compatible with csstype.

const x: CSS.Properties<string | number> = {} as React.CSSProperties; // OK

from csstype.

pelotom avatar pelotom commented on August 16, 2024

It's still not quite compatible when assigning style literals because of the missing SVG properties. I have a branch of @types/react where I define type CSSProperties = CSS.Properties<string | number> here:

https://github.com/pelotom/DefinitelyTyped/tree/react-csstype

Running npm run lint react produces the following errors:

Error: /Users/tom/code/DefinitelyTyped/types/react/test/cssProperties.tsx:41:49
ERROR: 41:49  expect  TypeScript@next compile error:
Type '{ fillOpacity: number; }' is not assignable to type 'Properties<Key>'.
  Object literal may only specify known properties, and 'fillOpacity' does not exist in type 'Properties<Key>'.
ERROR: 44:51  expect  TypeScript@next compile error:
Type '{ strokeOpacity: number; }' is not assignable to type 'Properties<Key>'.
  Object literal may only specify known properties, and 'strokeOpacity' does not exist in type 'Properties<Key>'.
ERROR: 47:49  expect  TypeScript@next compile error:
Type '{ strokeWidth: string; }' is not assignable to type 'Properties<Key>'.
  Object literal may only specify known properties, and 'strokeWidth' does not exist in type 'Properties<Key>'.

One escape hatch would be to just allow any arbitrary additional properties:

export interface CSSProperties extends CSS.Properties<string | number> {
  [k: string]: any;
}

which is what @types/react is already doing. TypeStyle takes the approach of exhaustively enumerating all properties, including the SVG ones:

https://github.com/typestyle/typestyle/blob/0ba077e53afdcd15ca6e9e26c17e7f1be8de5343/src/types.ts#L1871-L1875

(By hand? πŸ€·β€β™‚οΈ) What do you think is the best approach? I personally would prefer exhaustiveness, but I can understand that that's annoying to maintain if it has to be done manually.

from csstype.

frenic avatar frenic commented on August 16, 2024

Sure, I didn't mean fully compatible. But at least there's no mismatch on common properties with [email protected] as the previous version had on justifyContent.

I agree that the missing SVG properties is a problem because the whole idea is to have as perfect types as possible so index signatures aren't needed. I was thinking about writing them manually since they are not that many SVG properties. I don't see that as a problem. But I'm not sure how to include them in the Properties interface. It would be nice if you could do something like this to include properties that only applies to certain elements:

const style: CSS.Properties<Element> = {
  fill: 'red', // OK
  stopColor: 'green', // OK
};

const htmlStyle: CSS.Properties<HTMLElement> = {
  fill: 'red', // Error
  stopColor: 'green', // Error
};

const svgStyle: CSS.Properties<SVGElement> = {
  fill: 'red', // OK
  stopColor: 'green', // OK
};

const svgStopStyle: CSS.Properties<SVGStopElement> = {
  fill: 'red', // OK
  stopColor: 'green', // OK
};

const svgRectStyle: CSS.Properties<SVGRectElement> = {
  fill: 'red', // OK
  stopColor: 'green', // Error
};

Now the first generic argument is occupied. But in theory.

This would be possible with TypeScript conditional types that will be released in v2.8. But the problem is that Flow doesn't have any feature like that.

Including them all under Properties would of course be at least something and a good start.

from csstype.

frenic avatar frenic commented on August 16, 2024

I hear you πŸ‘ SVG properties should at least be separated but included in Properties like vendor properties.

from csstype.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.