typing

How to use (mqtt) js library in angular 2 typescript app?

浪子不回头ぞ 提交于 2019-12-04 06:07:26
I closely paralleled the approach taken in how-to-use-moment-js-library-in-angular-2-typescript-app but still get error TS2307: Cannot find module 'mqtt'. npm install --save mqtt <s>typings install --save mqtt</s that didn't find the typings but this did... typings install mqtt --save --ambient my tsconfig.conf looks like this { "compilerOptions": { "noImplicitAny": true, "module": "commonjs", "target": "ES5", "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, "declaration": true }, "files": [ "ng2-mqtt.ts" ], "exclude": [ "node_modules" ] } and ng2-mqtt.ts just

Typescript return type depending on parameter

家住魔仙堡 提交于 2019-12-04 03:53:30
问题 I am trying to write a function which takes a parameter of type boolean and returns one of two types, depending on the value of the input. I have found two approaches: function dependsOnParameter<B extends boolean>(x: B): B extends true ? number : string { if (x) { return 3; } else { return "string"; } } Here, TypeScript says that Type '3'/'"string"' is not assignable to type 'B extends true ? number : string'. My other approach looks like this: function dependsOnParameter(x: true): number;

Clean way to map objects to other objects?

有些话、适合烂在心里 提交于 2019-12-04 03:49:59
问题 Is there a good way to map objects to other objects? Library recommendations welcome too. For example, say I have these classes: export class Draft { id: number; name: string; summary: string; } export class Book { id: number; name: string; info: Info; } export class Info { value: string; } Traditionally, to map fields from Draft to Book I'd have to do so manually for each field: export class Book { [...] fromDraft(Draft draft) { this.id = draft.id; this.name = draft.name; this.info = new

python typing module missing the Coroutine class in python 3.5

心不动则不痛 提交于 2019-12-04 03:39:12
I am trying to write code which uses the Coroutine class, as described in the typing documentation . It's look like it's available in python 3.5 , but when I am typing to import it throws me an ImportError : In [1]: from typing import Coroutine ImportError: cannot import name 'Coroutine' Then, I tried to run the code in Python 3.6 and it worked fine. Does this class not available in python 3.5 ? If not, why it's appear in the documentation (of python 3.5 in particular)? I tried to run it with python 3.5.2 . The library typing was not official on 3.5, but became official on 3.6. So for older

Typehints for Sized Iterable in Python

﹥>﹥吖頭↗ 提交于 2019-12-04 02:52:20
问题 I have a function that uses the len function on one of it's parameters and iterates over the parameter. Now I can choose whether to annotate the type with Iterable or with Sized , but both gives errors in mypy . from typing import Sized, Iterable def foo(some_thing: Iterable): print(len(some_thing)) for part in some_thing: print(part) Gives error: Argument 1 to "len" has incompatible type "Iterable[Any]"; expected "Sized" While def foo(some_thing: Sized): ... Gives error: Iterable expected

Array of a generic class with unspecified type

安稳与你 提交于 2019-12-04 00:40:24
问题 Is it possible in C# to create an array of unspecified generic types? Something along the lines of this: ShaderParam<>[] params = new ShaderParam<>[5]; params[0] = new ShaderParam<float>(); Or is this simply not possible due to C#'s strong typing? 回答1: It's not possible. In the case where the generic type is under your control, you can create a non-generic base type, e.g. ShaderParam[] params = new ShaderParam[5]; // Note no generics params[0] = new ShaderParam<float>(); // If ShaderParam<T>

TypeScript: Reference subtype of type definition (interface)

只谈情不闲聊 提交于 2019-12-04 00:15:58
问题 I am using the following type in my TypScript: interface ExerciseData { id : number; name : string; vocabulary : { from : string; to : string; }[]; } Now I'd like to create a variable that is of the same type as the attribute vocabulary , trying the following: var vocabs : ExerciseData.vocabulary[]; But that is not working. Is it possible to reference to a subtype somehow? Or would I have to do something like this? interface ExerciseData { id : number; name : string; vocabulary : Vocabulary[]

Python type annotations for Enum value

别说谁变了你拦得住时间么 提交于 2019-12-03 22:34:13
I have this piece of code: import enum class Color(enum.Enum): RED = '1' BLUE = '2' GREEN = '3' def get_color_return_something(some_color): pass How do I properly add type annotations to the some_color varaible in this function, if I suppose to receive a value from the Color enum (for example: Color.RED )? Type hinting the Color class should work: def get_color_return_something(some_color: Color): print(some_color.value) def get_color_return_something(some_color: Color): pass 来源: https://stackoverflow.com/questions/52624736/python-type-annotations-for-enum-value

typing module - String Literal Type [duplicate]

≯℡__Kan透↙ 提交于 2019-12-03 11:29:03
This question already has answers here : Type hint for a function that returns only a specific set of values (3 answers) I'm using the new Python 3.5 module typing and it has been joyous. I was wondering how one might specify a type based on an exact string literal. For example, a function is guaranteed to return one of the four strings - "North", "West", "East", "South - how can we express that as a specific type variable, instead of just str . I looked through the documentation, finding the Union type and the TypeVar function, but was unable to find an answer. An example function expressing

PHP 7: use both strict and non-strict type hinting?

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: So PHP 7 has scalar type hinting now (w00t!), and you can have the type hints be strict or non-strict depending on a setting in PHP. Laracasts set this using define, IIRC. Is there a way to have strict type hinting on scalars in one file (like a math library) while at the same time using non-strict elsewhere WITHOUT just arbitrarily changing settings in your code? I'd like to avoid introducing bugs by not fidgeting with the language settings, but I like this idea. 回答1: Indeed, you can mix and match to your heart's content, in fact