uselesspickles / ts-enum-util Goto Github PK
View Code? Open in Web Editor NEWStrictly typed utilities for working with TypeScript enums
License: MIT License
Strictly typed utilities for working with TypeScript enums
License: MIT License
D:.
│ LICENSE
│ package.json
│ README.md
│
├─dist
│ └─types
│ index.d.ts
│ index.d.ts.map
│
└─src
index.ts
forgot push or build js file??
I'm using ts-enum-util: "^3.1.0. The error occurs in index.d.ts and is as follows:
Error: ERROR in node_modules/ts-enum-util/dist/types/index.d.ts(15,23): error TS2304: Cannot find name 'Extract'.
Until now, I'm gonna stick to Object.Keys..
Trying to use ts-enum-utils to describe types imported by typeorm.
The error I receive is
import { $enum } from 'ts-enum-util'
^^^^^^
SyntaxError: Cannot use import statement outside a module
The tsconfig.json file was generated by nestjs\cli and looks like this.
{
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"resolveJsonModule": true
},
"exclude": ["node_modules", "dist"]
}
What am I doing wrong?
Motivation:
In numerous scenarios, the order of the values is more important than lexicographical keys.
for example:
import React, {PureComponent} from "react";
import { $enum } from "ts-enum-util";
enum Log {
debug,
info,
warning,
error
}
class LogSelector extends PureComponent{
public render() {
return <select>{$enum(Log).map((value, key) => <option key={value}>{key}</option>)}</select>;
}
}
The select
will have the options ordered debug, error, info, warning
which is harder to understand than debug, info, warning, eror
.
Maybe add something like $enum(Log, true)
to order by value? or passing comparable?
Thanks
Just curious, why naming your export member $enum
in your examples? Does this mean anything?
Anyways, thanks, useful library 👍
I just checked the src/ directory and fount no file import typescript.
It would be great if it can be removed peer dependencies list.
I have the following code
export enum DecisionType {
DECISION = 'whatever,
UNDEFINED = 'undef'
}
And I want to make sure I receive a string that is one of the legal value and return the correct key.
If I understand correctly, I should be doing this
export function DecisionTypeString (decisionStr: string): StringKeyOf<DecisionType> {
let value = $enum(DecisionType).asValueOrDefault(decisionStr, DecisionType.UNDEFINED)
let retval = $enum(DecisionType).getKeyOrDefault(value, DecisionType.UNDEFINED)
return retval
}
However, I get the following error message
CaseEnums.ts:15:3 - error TS2322: Type 'string' is not assignable to type '"toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | ... 29 more ... | "trimRight"'.
What am I doing wrong?
I want to get the proper key (mixing upper and lower cases) from a string (lower case).
How can I do without looping through the whole array?
Here is my current implementation. Any better like using asKeyOrDefault
?
enum Languages {
'unknown',
'en',
'zh-TW',
'zh-CN',
'ja',
}
const key = $enum(Languages).getKeys().find(x => x.toLowerCase() === 'zh-tw')
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.