Closed nojvek closed 4 years ago
I think this is just #7481 - you'd write { border: 1 } as const __some_op__ CSSproperties
?
I guess. Does it involve making a new operator though ? You already have the x extends Y syntax for function generics. Iβd imagine it would be somewhat similar.
Feel free to close it if itβs already on your radar.
I think a new operator is the only right way to do it. The proposed syntax above is hard to reason about in higher-order cases (where it doesn't apply anyway), so the type hinting should be taking place more at an expression level
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
Search Terms
const extends type, const typed object
Suggestion
const<T>
syntax would mean I want the const inferred type, but ensure that it extendsT
.Use Case
when writing a normal object definition e.g
const css = {border: '1px solid red', padding: '2px'}
, typescript correctly infers the type ofcss
as{border: string, padding: string}
.However sometimes you still want the inferred type to be a const, but ensure it extends another interface. e.g
I am working with css in js. Where the css tree is written using js/ts syntax.
The top level keys are class names, the values are css properties. Consumed in tsx file as
What this achieves is that typescript gives great completion when typing
styles.X
and enforces that the class name is actually defined in jss.ts file. This means the usage in tsx is bound to definition in jss.ts file. Also ctrl+clickstyles.xyz
correctly takes to where the css is defined.What would be nice to be added
Right now with just
const jss ={...}
, TS can't enforce that the properties are CSSProperties.but doing the below would be awesome.
This would mean jss would fully retain the const shape and doing
{[C in keyof T]: string}
would still work.Currently one way to achieve this would be two write a dummy function that does nothing but just signals typescript that
T extends SomeType
e.gand you'd use it like
What is the ask ?
Rather than having to pass everything to an identity function like
use
const<T>
syntax insteadChecklist
My suggestion meets these guidelines: