-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Runtime type checking #1573
Comments
Perhaps something more akin to google |
I'm suggesting, however, that you can optionally enable runtime type checking on some args and not others. Or, at least, skip type checking on private methods, optionally. |
This remains outside our design goals (https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals) |
For people like me coming here looking with high hopes, you might want to take a look at http://angular.github.io/assert/ |
Well, this can't be very complex to implement since you already have the infrastructure for type checking. At least you could expect to get injected type checking for primitive types when compiling with --debug, as mentioned in the other thread:
If this does not match your design goals, maybe you should change them? |
Primitive TypeChecking is easy, and the results of doing it yourself guide type inference. If you expect to receive something other than the indicated type, perhaps you should not specify |
I would expect to get a runtime error if a function receives eg a string instead of a number in runtime. That would really kill the flaws of working with JavaScript. Don't get me wrong, I love TypeScript, but I really hate JavaScript.. |
But, IMO, you need to learn to love JavaScript to fully take advantage of TypeScript. |
Well I think that is mission impossible for me. JavaScript is great for what is was made for, but for large complex systems built and maintained by a team I can't see how it could be a good modern option. @jedmao Thank you, will have a look. But I still think it should be implemented in the TypeScript compiler (with a flag) |
@RyanCavanaugh it's been almost 2 years since this feature request was made and I'm wondering what it would take for the design goals to change or make an exception for this particular request? Now that the initial TypeScript roadmap is complete, perhaps we can entertain this idea? I think this proposed compiler flag would only make sense in development and staging environments, but it would serve as a great tool for debugging the application at runtime. Consider the scenario in which an API request is made and we expect the API response to adhere to a model (interface). interface Foo {
bar: string;
}
function handleResponse(data: Foo) {
return data;
} Compiles to: const __Foo = {
validate: (d) => {
if (typeof d.bar !== 'string') {
throw new TypeError('Foo.bar should be a string.');
}
}
};
function handleResponse(data) {
Foo.validate(data);
return data;
}
handleResponse({ bar: 'baz' }); // OK
handleResponse({ bar: 42 }); // TypeError('Foo.bar should be a string.'); Rather than getting some error about some |
@jedmao I think this a natural next step for TypeScript |
@jedmao @jeansson If it is import reified from './reified';
export default class Model {
@reified bar: string;
@reified baz: number;
constructor(properties: Partial<Model> = {}) {
Object.assign(this, properties);
}
}
const y = new Model({
baz: '42' as any, // throws at runtime
bar: 'hello'
}); reified.ts import 'reflect-metadata';
export default function <T extends Object>(target: T, key: string) {
const fieldKey = `${key}_`;
console.log(target[fieldKey]);
const fieldTypeValue = getTypeTestString(Reflect.getMetadata('design:type', target, key));
Object.defineProperties(target, {
[key]: {
get() {
console.log(`${key}: ${fieldTypeValue} = ${this[fieldKey]}`);
return this[fieldKey];
},
set(value) {
if (fieldTypeValue && typeof value !== fieldTypeValue) {
throw TypeError(`${fieldTypeValue} is incompatable with ${typeof value}`);
}
this[fieldKey] = value;
}, enumerable: true, configurable: true
}
});
}
function getTypeTestString(type) {
switch (type) {
case Number: return 'number';
case String: return 'string';
default: return undefined;
}
} Obviously this doesn't work for complex types. |
@aluanhaddad, your solution would add footprint to the compiled output. The idea here is that it would be a compiler flag you could turn off for production builds and that it would work with complex types, not just classes. |
I am not recommending it as a solution. |
@aluanhaddad The whole point is that I should not be forced to check this manually in a typed language, so the type checking needs to be injected automatically at compile time if the flag is set. It also prevents the overhead @jedmao mentioned because the runtime type checking will not be injected when you build the release version. Why do you not like this solution? |
Look https://github.com/codemix/babel-plugin-typecheck. It could work like this but automatically. Edit: found same tip: #7607 (comment) |
Rather than building runtime type checking into the compiler - an alternative might be to expose type metadata to runtime code (e.g. via |
This lib came across my slack this morning - although designed for Facebook's flow it states it could work with Typescript. It is built upon the work of the babel-plugin-typecheck that @GulinSS mentioned. |
I can't understand why this is out of the scope of typescript, which adds exactly this kind of things to JS. Seems that it would be a better idea to use flow + flow-runtime instead. What a pity because I like typescript. |
I just did a proof of concept using TypeScript. Only enforcing type checking at compile time and not run-time in JavaScript is dangerous. You are essentially tricking developers into thinking they are writing Type safe JavaScript, when in fact, the moment any data from an outside system ( like the client ) comes into a "Typed" function you are going to be |
Oh I see, cool! |
@RyanCavanaugh Please revisit this, and especially consider doing it in a non-production environment, or always doing it when encountering a Perhaps the design goals could be expanded to include this? |
From the goals
This is ironic, given than TypeORM relies heavily on runtime metadata about decorators AFAIK. The problem with this goal is there's just no better way to define types for validating things like JSON documents than with the syntax of TypeScript/Flow itself. Until runtime type introspection becomes common, we'll be stuck with an excess of crappy validation libraries that are nowhere near as elegant. |
This seems like a perfect use case for typescript macros 👍 |
So if I'm building a library that can be adopted by plain javascript users, it's prone to weird errors for them. It'd be cool to have a flag for JS builds to do complete type checking. So if users are using TS, they don't get these type checks. If they are using the JS build, they get them. |
Not any more prone than any regular Javascript library. Actually, Javascript users can trust code generated by the Typescript compiler a lot more. The real usefulness lies in localized dynamic type checks for the reverse situation. When Typescript code must use data coming from an untrusted (more specifically non type checked) environment (that means from Javascript code, or from network transmitted JSON) it could definitely use a statically auto-generated dynamic type check at the receiving location. |
Perhaps I am beating a dead horse, but decorator metadata does not represent types. It embeds values based on compile time types that correspond to conveniently named (if conflation is desired) runtime values. https://github.com/fabiandev/ts-runtime uses an orthogonal approach and with good reason. |
@aluanhaddad I don't see a good reason. C# is strict typed at runtime. |
I build a very simple runtime validation library for this purpose as well that I use for validating JSON api requests. https://github.com/ccorcos/ts-validator At the heart of it is this simple validator type: export type Validator<T> = (value: T) => boolean But then you can create compositions of this type: type Purify<T extends string> = { [P in T]: T }[T]
export type ObjectSchema<T extends object> = {
[key in Purify<keyof T>]: Validator<T[key]>
} And at the end of the day, you can a create type-safe runtime validation functions at are 1:1 with the type interface: import * as validate from "typescript-validator"
interface User {
id: number,
name?: string,
email: string,
workspaces: Array<string>
}
const validator = validate.object<User>({
id: validate.number(),
name: validate.optional(validate.string()),
email: validate.string(),
workspaces: validate.array(validate.string())
})
const valid = validator({id: 1, email: "hello", workspaces: []}) Would be cool if these functions could be generates in typescript, but I suppose its not that necessary. |
@saabi Actually the syntax can be much more easier by using interface Employee {
name: string;
salary: number;
}
function webServiceHandler ( possibleEmployee: any ) {
try {
const actualEmployee = possibleEmployee as Employee;
// Code for type assertion should be generated when the compiler
// spot that we are casting `any` to a specific type
}
catch (e) {
// validation failed;
}
} |
@wongjiahau That would have to be optional, it's currently the best way to access properties on objects that don't have them in their type. Unfortunately it's quite common when interfacing with other javascript or global objects on web pages and this check would break them. example function setup(stack: any[]) {
if ((stack as any).setupDone) {
return;
}
// ...
(stack as any).setupDone = true;
} |
Anyway, will anyone support me if I'm going to implement this feature by forking this project? |
Apologies, my example used a non-any type when you specified casting any -> non-any. Still, it's a backwards-incompatible change which means you're fighting an uphill battle. That's why other people have suggested new syntax such as |
@wongjiahau I wouldn't necessarily want all "as" operators to create type checking code around it. I'd much rather be explicit with it with a simple |
@jedmao I did look at @tbillington Ok now I understand your concern (of the backwards-compatibility). So, I guess the syntax should looks like this as suggested by @jedmao? function square1(x: number!) {
// TS compiler will generate code that will assert the type is correct
return x * x;
}
function square2(x: number) {
// TS compiler will not generate any type-assertion code
return x*x;
}
var apple: any = "123";
square1(apple); // Compiler will not throw error, since the type of `apple` is `any`.
// But this code will cause error to be thrown at runtime
square2(apple); // Compiler will throw error
var banana = "123";
square1(banana); // Compiler will generate error because you can't cast string to number by any means So, the @jedmao @tbillington What do you think? |
@wongjiahau my opinion is that the compiler errors should be exactly as they are w/o change. The only difference is runtime. As such, I don't see how the Let's take your example: function square1(x: number!) {
// TS compiler will generate code that will assert the type is correct
return x * x;
} The TS compiler would simply emit something akin to the following: function square(x) {
if (typeof x !== 'number') {
throw new TypeError('Expected a number.');
}
return x * x;
} Super easy with primitive types, but more complicated with interfaces and more complex types (but io-ts solves that issue). I just wish it were built into the compiler itself so we didn't have to do all this extra stuff. But it really is complex and requires a lot of thought behind it in order to do it right. Here's some other ideas I thought of recently: // First line of file
const x: number! = 42; // compiler error: redundant type checking on a known type. const x: number = 42;
const y: number! = x; // also redundant const x: number = 42;
const y: string! = x; // compiler error:
const z: string! = x as any; // OK, but should it be? There's really a lot to think about here. |
@jedmao I understand what you say, but please allow me to clarify the error suppression with a typical example. Suppose you have the following code (using interface Fruit {
name: string,
isTasty: boolean
}
app.post("/uploadFruit", (req, res) => {
const newFruit = req.body; // Note that `req.body` has type of `any`
// This line shouldn't throw compile error because we already tell the compiler
// to assert that newFruit is type of `Fruit` by adding the `!` operator
saveFruit(newFruit);
});
function saveFruit(newFruit: Fruit!) {
// save the fruit into database
} If that line of I hope you can understand what I'm trying to say. Moreover, I think that syntax A will be better than syntax B // syntax A
const apple!: string = "...";
// syntax B
const apple: string! = "..."; This is because syntax A will be much more easier to be parsed, and it is also more consistent with the So you can read the |
@wongjiahau I'm not suggesting that That said, I still don't see where any error is being "suppressed" as you say. I do kinda' like the function foo(x!: string | number) {} |
@jedmao Sorry for the misunderstanding, because I thought the compiler will throw error if we cast from So, do you guys want to fork this project and implement this feature? |
I think Microsoft would have to agree to accept a community PR for me to invest time in this. Also, I just don't have the bandwidth. |
@jedmao I'm actually thinking of forking it as another project (perhaps I'll call it Experimental-Typescript ? ). |
Since we're talking a lot about validation of unsafe data, I think Phantom types are relevant: https://medium.com/@gcanti/phantom-types-with-flow-828aff73232b Currently, its not possible in Typescript. |
I see some people made libraries for runtime checking but I would show another one (created by myself): However I'm planning to add features like optional errors throwing, global onError callbacks, constraints, json or function resolvers. then it would be more for runtime checking |
Creator of Node.js seems to be working on similar project for TypeScript: https://github.com/ry/deno "A secure TypeScript runtime on V8" |
Interesting. I checked out the repo -- still pretty unclear what it's for... |
I request a runtime type checking system that perhaps looks something like this:
Where the
!
tells the compiler to generate a runtime type check for a number, something akin to tcomb.js.Of course, this gets much more complicated with interfaces, but you get the idea.
The text was updated successfully, but these errors were encountered: