我想定义一个可在任何地方使用的全局函数,而无需在使用时导入模块.
此功能旨在替换C#中提供的安全导航操作符(?).为了便于阅读,我不想在函数前加上模块名称.
Global.d.ts:
declare function s<T>(someObject: T | null | undefined, defaultValue?: T | null | undefined) : T;
Run Code Online (Sandbox Code Playgroud)
Global.tsx:
///<reference path="Global.d.ts" />
export function s<T>(object: T | null | undefined, defaultValue: T | null = null = {} as T) : T {
if (typeof object === 'undefined' || object === null)
return defaultValue as T;
else
return object;
}
Run Code Online (Sandbox Code Playgroud)
App.tsx(root TypeScript文件):
import 'Global';
Run Code Online (Sandbox Code Playgroud)
其他TSX文件(方法用法):
s(s(nullableVar).member).member; //Runtime error
Run Code Online (Sandbox Code Playgroud)
这编译很好,但是,在浏览器中抛出' s is not a function'.
我想强输入属性名称
myMethod(model => model.userId);
public myMethod(model: () => any) {
//Must print "userId"
}
Run Code Online (Sandbox Code Playgroud)
我已经知道这行不通,因为 JavaScript 会评估 userId。
在 C# 中很容易做到:
是否可以在 TypeScript/JavaScript 中做到这一点?
在JavaScript中我们可以这样做:
var Color = {
YELLOW: { value: 1, displayString: "Yellow" },
GREEN: { value: 2, displayString: "Green" },
}
Run Code Online (Sandbox Code Playgroud)
所以我可以打电话:
Color.YELLOW.displayString
Run Code Online (Sandbox Code Playgroud)
在Java中我们可以这样做:
public enum Color {
YELLOW (1, "Yellow"),
GREEN (2, "Green"),
private Color(String value, int displayString){
this.value = value;
this.displayString = displayString;
}
private final int value;
private final String displayString;
public String getValue() {return value;}
public String getDisplayString() {return displayString;}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以打电话:
Color.YELLOW.getDisplayString()
Run Code Online (Sandbox Code Playgroud)
经过大量研究后,我还没有找到一种使用内置Enum模块在Python中执行此操作的简洁方法.我怎样才能做到这一点?
谢谢
我正在尝试修补由我尝试测试的类实例化的类,但它不起作用.我已经阅读了各种文档,但仍然没有找到我做错了什么.这是代码片段:
在tests/Test.py:
from module.ClassToTest import ClassToTest
class Test(object):
@mock.patch('module.ClassToPatch.ClassToPatch', autospec = False)
def setUp(self, my_class_mock):
self.instance = my_class_mock.return_value
self.instance.my_method.return_value = "def"
self.class_to_test = ClassToTest()
def test(self):
val = self.class_to_test.instance.my_method() #Returns 'abc' instead of 'def'
self.assertEqual(val, 'def')
Run Code Online (Sandbox Code Playgroud)
在module/ClassToPatch.py:
class ClassToPatch(object):
def __init__(self):
pass
def my_method(self):
return "abc"
Run Code Online (Sandbox Code Playgroud)
在module/ClassToTest.py:
from module.ClassToPatch import ClassToPatch
class ClassToTest(object):
def __init__:
# Still instantiates the concrete class instead of the mock
self.instance = ClassToPatch()
Run Code Online (Sandbox Code Playgroud)
我知道在这种情况下我可以轻松地注入依赖项,但这只是一个例子.此外,我们使用单个类的每个文件策略,文件名称类,因此奇怪的导入命名.
我正在尝试运行一个批处理文件,该文件运行可执行文件并将其输出重定向到日志文件。日志文件必须将日期和时间作为文件名。这是我正在使用的命令:
"%PROGRAMFILES%\PostgreSQL\9.4\bin\vacuumdb.exe" --username postgres --verbose --analyze --all > E:\Logs\VacuumDB\%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%.log 2>&1
Run Code Online (Sandbox Code Playgroud)
此命令在直接粘贴到 cmd 中时有效。日志文件按预期创建为“2015_06_25__11_20_46.log”。但是,粘贴到批处理文件中,然后在cmd中运行时,它不起作用。Cmd 对命令的解释如下:
"C:\Program Files\PostgreSQL\9.4\bin\vacuumdb.exe" --username postgres --verbose --analyze --all 8_21_42.log 1>E:\Logs\VacuumDB\2015_06_26_ 2>&1
Run Code Online (Sandbox Code Playgroud)
注意文件名是如何被截断的,时间现在被附加到命令参数而不是文件名中。所以显然命令失败了。
这肯定是非常简单的事情,但我还没有找到任何方法来解决这个问题。任何帮助是极大的赞赏。
谢谢!
鉴于此功能:
myFunc(object: string | null): string | null {}
Run Code Online (Sandbox Code Playgroud)
我想这个函数string在对象是的时候是string返回类型,并且当对象是类型时返回string | null类型string | null.
我试过了:
myFunc<T extends string | null>(object: T): T {
return "returnValue"; //Type '"returnValue"' is not assignable to type 'T'.
}
Run Code Online (Sandbox Code Playgroud)
和
myFunc<T extends string & null>(object: T): T {
return "returnValue"; //Type '"returnValue"' is not assignable to type 'T'.
}
Run Code Online (Sandbox Code Playgroud)
两者都产生相同的编译错误.我没有找到正确的语法来执行此操作.
typescript ×3
python ×2
python-3.x ×2
batch-file ×1
cmd ×1
date ×1
javascript ×1
logging ×1
windows ×1