-
Notifications
You must be signed in to change notification settings - Fork 885
Conversation
Just a thought, what do you think about banning
is preferred. A The other advantage is that this wouldn't need the type-checker, so it could be used in more situations and would be faster. |
On the other hand, a general ban of |
Unfortunately I don't think so, check out the description of Maybe @weswigham could shed some insight on the best way to check if something is an Array type? |
So is that saying that an object of a generic type (say function foo<T>(promise: Promise<T>) {
for (var k in promise);
} But I typically use for-in for iterating over object literals that I've created myself, so inherited properties aren't a big concern. for-in is compact and seems to be faster than for-of-Object.keys in modern browsers and node. I can see the argument for disallowing it entirely, though. Maybe there should be two options for a "for-in" rule: |
When testing the type of class A<T> { }
let b: A<number>; I get a result that shows that
In case it helps, here's the method I added to your rule to get this output: public visitVariableDeclaration(node: ts.VariableDeclaration) {
const tc = this.getTypeChecker();
const type = tc.getTypeAtLocation(node);
// tslint:disable
const isReference = (type.flags & ts.TypeFlags.Reference) > 0;
console.log(`${isReference ? "Reference" : "Not Reference"}: ${node.getText()}`);
// tslint:enable
} |
If we go your route for this rule, we could actually just enhance the current I know I proposed it above in the thread, but after more thought, I don't think a rule that warns about |
Sure, I could add an I pushed a new version of the array check. The logic is now: const isArrayType = type.symbol && type.symbol.name === "Array"; This handles the |
(If this check seems correct then I'll add it to the existing rule.) |
Your guess is as good as mine here, but this makes sense to me and can't think of any edge cases which would mess things up. |
function foo() {
type Array = MyArray<T>;
let x = new Array();
// ...
} It doesn't handle block scoped type declarations whose name is also |
Ha, are there any better alternatives? Let local type declarations named |
If you redefine |
@jkillian You want to be able to lookup the global array type, then check if the type you find at that location is an instantiation of it. Inside the compiler, this wouldn't be too difficult, but we don't publicly expose the functions you need to do this check on the type checker instance right now. Heck, we don't even expose a way to lookup a global type! I've known about this shortcoming in our public API since I started working on compiler extensions and have had it at the back of my mind. I've opened #9879 to track the issue. |
@jkillian I pushed a new version which merges my This introduces a new problem, though (and it's the reason that the tests will fail): the rule now requires type checking, whereas it didn't before. So existing configs will break. What do you think? Is it still a good idea to merge the rules? |
@weswigham Thanks for the info, sounds like the current method we're using is best for now then. Excited for the Type Relationship API! (And the other ones, like the Type Builder API). @danvk Shoot, wasn't thinking about that. We definitely don't want to break existing users. Let's just keep it as |
@jkillian I've rolled this back to the version with a separate no-for-in-array rule. I believe this is ready to go. |
Thanks @danvk! |
* no-for-in-array rule * Change array check * Add typeCheck linterOption
Fixes #1380
I would be interested in feedback on whether this is accurate:
The
typescript.d.ts
file is quite terse about what a "Generic type reference" is, but in my testing it seems to correspond precisely to array types.