Comments (4)
hi everyone,
I've added a transformer option functionBehavior
in v0.17.0:
https://github.com/woutervh-/typescript-is/releases/tag/v0.17.0
This option can take the values basic
, ignore
or error
.
error
is like ignoreFunctions: false
, it will throw compile-time errors when functions are found.
ignore
is like ignoreFunctions: true
, it will create a validation which always passes.
basic
is new, and it will create a validation which does a minimal typeof X === 'function'
check.
Hope this helps you with functions.
Regarding classes, I'm still not decided on what the best approach is, but feel free to join the discussion here #20
from typescript-is.
Hi @timse
Yes, it's possible to add a check for methods and function, for example to generate typeof X === 'function'
.
May I know more about your use case? I'm wondering if many people need this functionality or not.
from typescript-is.
This would be ideal. As it stands it seems to fail in this case
import { is } from 'typescript-is';
type MyType = {
one: string;
two: number;
};
type MyTypeFn = {
two: () => any;
};
function run(obj?: undefined | MyType | Partial<MyType> | MyTypeFn) {
if (is<MyType>(obj)) {
return obj.one;
}
if (is<MyTypeFn>(obj)) {
return obj.two;
}
}
console.log(run({ one: 'hi' }));
console.log(run({ one: 'hi', two: 2 }));
console.log(run({ two: () => console.log('yep') }));
console.log(run({}));
with the results:
undefined
hi
undefined
undefined
I would expect that the is<MyTypeFn>
to mean the response here would be the function itself. but it is returning undefined. I have enabled ignoreFunctions which it said would just return true here... but either way it makes sense that if the checker finds a function type in the type it is checking for it to at least just do a typeof value === 'function'
check and have a flag to enable or disable.
It is less than ideal that any type that happens to have a function in it makes this fail completely.
Other than that this is an absolutely awesome module and thank you!
from typescript-is.
I have the same request.
My use case is that I am doing runtime-type-checking of all arguments passed between the frontend and backend of an Electron. app (the data passes through a context-bridge, since I have context-isolation turned on)
I do these generic runtime-type-checks as an extra precaution, in case at some point the frontend portion of my app became compromised; the idea is to limit the chances the frontend could perform harmful effects on the NodeJS side, by limiting its options for crafting unexpected arguments that would trick the backend functions into performing harmful actions. (higher-level vulnerabilities I already handle through custom assertions)
Because these are just precautionary runtime-type-checks, I cannot show clear examples of cases where having an "is function" check is important. However, every bit that you can "tighten the requirements", is a win for constraining the manipulation options of the theoretically-compromised frontend.
EDIT: In response to the possible question: "Why wouldn't someone just write typeof argX == function
checks manually, instead of is<Function>(argX)
-- since it's about the same length?"
Well, one reason is that it's nice to have all parameters using the same "@AssertType()" decorators for enacting the type-checking.
The other reason is that some of the functions also receive objects matching nested interface types. With @AssertType()
, I get automatic type-checking of the whole interface/object-tree (regardless of depth of the function property); whereas if I used manual typeof
checks for them, I would have to recreate the type-checking tree. (or somehow "extract out" the nested function property, eg. setting it to any
within the interface structure, and creating manual tunnel-down-and-assert code for those properties)
from typescript-is.
Related Issues (20)
- Feature Request: Shortcut to simplify `const v = func(); is<type>(v) ? v : default` HOT 1
- Fix intersections with primitives to add support for branded/flavored types HOT 1
- Intersection disables `disallowSuperfluousProperties` all the way down
- Non-deterministic code generated HOT 1
- Support typeof on object types
- Parsing fails when target & aliasTypeArguments are at different levels HOT 1
- Investigation of Unbound Type Parameters
- typescript-is introduces "typescript" dependency into production environment
- TypeError: Cannot read property 'text' of undefined
- Bug: A missing key and a key with a value of undefined are not considered the same
- Fail to check Enum in Record HOT 9
- Have you plan to support Deno? HOT 2
- use instantiation expressions in typescript 4.7
- Make generated code smaller by putting reusable strings to constants or functions HOT 1
- feature request: set custom error class in AssertType
- Thoughts on extending createAssertType() HOT 1
- Incompatible with TypeScript 4.8 HOT 2
- What about re-export `typia` for maintenance? HOT 1
- ttypescript not support TS@5
- Deprecation message and recommends to use `typia` instead HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript-is.