typing

Typescript “error TS2532: Object is possibly 'undefined'” even after undefined check

五迷三道 提交于 2019-11-29 13:12:40
I'm trying to use the --strict option on tsc but I ran into the following "weird" case that I don't seem to understand. If I write: function testStrict(input: {query?: {[prop: string]: string}}) { if (input.query) { Object.keys(input.query).forEach(key => { input.query[key]; }) } return input; } the compiler complains about: test.ts(5,9): error TS2532: Object is possibly 'undefined'. (the offending line is input.query[key]; ) What I don't understand is, I have already checked for undefined with the if guard on the first line of the function if (input.query) , so why does the compiler think it

Python 3 type hinting for decorator

半腔热情 提交于 2019-11-29 09:15:50
Considere the following code: from typing import Callable, Any TFunc = Callable[..., Any] def get_authenticated_user(): return "John" def require_auth() -> Callable[TFunc, TFunc]: def decorator(func: TFunc) -> TFunc: def wrapper(*args, **kwargs) -> Any: user = get_authenticated_user() if user is None: raise Exception("Don't!") return func(*args, **kwargs) return wrapper return decorator @require_auth() def foo(a: int) -> bool: return bool(a % 2) foo(2) # Type check OK foo("no!") # Type check failing as intended This piece of code is working as intended. Now imagine I want to extend this, and

Can't rely on target typing when chaining methods [closed]

只愿长相守 提交于 2019-11-29 08:02:06
In Java 8, type inference has been extended to target typing which enables to write: Comparator<String> ascending = comparingInt(String::length); without having to use a type witness ( Comparator.<String> comparingInt ). However the last statement below does not compile. Is there a reason? Is there a workaround? Comparator<String> ascending = comparingInt(String::length); //ok Comparator<String> descending = ascending.reversed(); //ok Comparator<String> descending = reverseOrder(comparingInt(String::length)); //ok Comparator<String> descending = Comparator.<String>comparingInt(String::length)

Dynamic typed ViewPage

自古美人都是妖i 提交于 2019-11-29 07:31:43
问题 Is this possible? Here's what I'm trying: public ActionResult Index() { dynamic p = new { Name = "Test", Phone = "111-2222" }; return View(p); } And then my view inherits from System.Web.Mvc.ViewPage<dynamic> and tries to print out Model.Name. I'm getting an error: '<>f__AnonymousType1.Name' is inaccessible due to its protection level So basically, is what I'm trying to do just not possible? Why or why not? Update: here's my view <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared

What is Dynamic Typing?

限于喜欢 提交于 2019-11-29 02:06:41
I've heard this term used with scripting languages such as PHP. What exactly does it mean? Dynamic typing is a definitive characteristic of a language. A short explanation might be: A language has dynamic typing when it does not associate values strictly with a specific type, but it is designed to "decide" what the type of a value should be at runtime, based on how you are attempting to use it. For example, in PHP you can write $count = "5"; // defines a string variable and then go on to say $count = $count * 2; // this is legal and has the obvious result¹ What happened here? For one, the

C# “is” operator - is that reflection?

元气小坏坏 提交于 2019-11-29 01:41:33
问题 A colleague asked me an interesting question today - is the C# keyword/operator "is" considered reflection? object tmp = "a string"; if(tmp is String) { } How is this operator implemented behind the scenes? Does it require reflection or introspection? Or because of the strongly typed nature of the language, is the Type of the object immediately accessable as a top-level attribute of the object in memory? MSDN states that: Note that the is operator only considers reference conversions, boxing

Dynamic typing, Objective-C, how does it work?

折月煮酒 提交于 2019-11-28 23:11:03
问题 I'm interested in how does dynamic typing in Objective-C work. I've been studying the "id" type, I know what it does and how to use it, but I'm curious... How does such functionality get implemented under the hood? You cannot determine/resolve anything during compile time, only during runtime. I guess it can simply just point to the first byte of some object in memory, but how is the class signature stored? How does it know what is it currently pointing towards and how does it implement

Is ruby strongly or weakly typed?

≯℡__Kan透↙ 提交于 2019-11-28 22:26:43
问题 Is ruby strongly or weakly typed ? Presumably the same is true for Javascript. 回答1: Ruby is " strong typed ". Strong typing means an object's type (not in the OOP sense, but in a general sense) is checked before an operation requiring a certain type is executed on it. Weak typed means that no checking is done to ensure that the operation can succeed on the object. (For example, when a function accesses a string like and array of floats, if no type checking is done then the operation is

typing animated text

家住魔仙堡 提交于 2019-11-28 19:41:40
I have DIV tag with text inside. Is it possible to change the text content in a loop with a typing effect, where it types out, then goes backward deleting the letters and starting all over with a new text? Is this possible with jquery? Just a simple approach: $("[data-typer]").attr("data-typer", function(i, txt) { var $typer = $(this), tot = txt.length, pauseMax = 300, pauseMin = 60, ch = 0; (function typeIt() { if (ch > tot) return; $typer.text(txt.substring(0, ch++)); setTimeout(typeIt, ~~(Math.random() * (pauseMax - pauseMin + 1) + pauseMin)); }()); }); /* PULSATING CARET */ [data-typer]

Is there a way to “extract” the type of TypeScript interface property?

回眸只為那壹抹淺笑 提交于 2019-11-28 18:33:45
Let's suppose there's a typing file for library X which includes some interfaces. interface I1 { x: any; } interface I2 { y: { a: I1, b: I1, c: I1 } z: any } In order to work with this library I need pass around an object that is of exactly the same type as I2.y . I can of course create identical interface in my source files: interface MyInterface { a: I1, b: I1, c: I1 } let myVar: MyInterface; but then I get the burden of keeping it up to date with the one from library, moreover it can be very large and result in lot of code duplication. Therefore, is there any way to "extract" the type of