Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tightened typing for Object Interpolation (CSSProperties) #644

Open
wants to merge 2 commits into
base: master
from

Conversation

@shqld
Copy link

@shqld shqld commented Jul 29, 2020

Motivation

Object Interpolation is very useful especially for creating sass-like mixins such as

function margin(px: number): CSSProperties {
  return { margin: px + 'px' };
}

const divStyle = css`
  ${margin(12)}
`;

However, currently CSSProperties type accepts any keys and values even outside of CSS rule stuff. This is problematic for type safety and usability (e.g. auto-completion).

Summary

In this PR, I've just replaced declaration of CSSProperties.

Concretely, replaced [key: string]: string | number with csstype.Properties which is responsible for covering all available key-value pairs of css property.

This allows us more relialbe type cheking and usable auto-completion for Object Interpolation without downside in runtime.

For extendability of property types, internal interface called Properties is exported so that users can merge interface at their discretion in the user-land.

Test plan

function marginTop(px: number): CSSPropreties {
  return {
    // OK
    marginTop: px + 'px',
    // OK
    'margin-top': 0,
    // ERROR
    'margin-top': 1,
    // ERROR
    'my-prop': 'hello',
  };
}

css`
  ${{
    // OK
    marginTop: px + 'px',
    // OK
    'margin-top': 0,
    // ERROR
    'margin-top': 1,
    // ERROR
    'my-prop': 'hello',
  }}
`;
declare module 'linaria/lib/core/css' {
  interface Properties {
    'my-prop': 'hello';
  }
}

function marginTop(px: number): CSSPropreties {
  return {
    // OK
    'my-prop': 'hello',
  };
}

css`
  ${{
    // OK
    'my-prop': 'hello',
  }}
`;
shqld added 2 commits Jul 29, 2020
@shqld shqld force-pushed the shqld:feat/tightened-typing-for-interpolated-object branch from aacd96a to 1721864 Jul 29, 2020
@callstack-bot
Copy link

@callstack-bot callstack-bot commented Jul 29, 2020

Hey @shqld, thank you for your pull request 🤗.
The coverage report for this branch can be viewed here.

@Anber
Copy link
Collaborator

@Anber Anber commented Aug 10, 2020

Hi @shqld!
Unfortunately, csstype doesn't work with CSS custom properties and it can be a problem for us. The problem looks unresolvable until that proposal isn't implemented.

@shqld
Copy link
Author

@shqld shqld commented Aug 18, 2020

Hi @Anber !Thanks for your reply.

We can specify any types we want by overriding types using TypeScript's Declaration Merging (as I described above), so I believe it will still have some effect to do something like in this PR without special support for custom properties. At least no disadvantage is considered, IMHO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.