我一直在开发一个身份验证服务,该服务使用 rxjs 行为主题来存储最后检索到的身份验证对象,并在该对象已过期(或根本尚未获取)时触发重新获取。
我的问题是关于 TypeScript 类型检查器。我已经编写了断言的类型保护程序isNotUndefined
- 嗯,正是您所期望的。
export function isNotUndefined<T>(input: T | undefined): input is T {
return input !== undefined;
}
Run Code Online (Sandbox Code Playgroud)
我已经不得不编写上面的 typeguard 而不是能够依赖auth !== undefined
. 我现在无法理解为什么在authGetter$
下面代码的管道中,管道中值的类型没有减少到Auth
第一个过滤器之后。相反,类型仍然是Auth | undefined
,并且需要第二个过滤器仅带有类型保护才能将类型缩小到仅Auth
。
总而言之,为什么我需要第二个过滤器将类型缩小到仅Auth
?此外,因为我是自己编码,没有人审查它,所以我非常感谢任何人指出他们认识到的“代码味道”(并提供有关如何处理的建议)。
export default class AuthService {
private static lastAuth$ = new BehaviorSubject<Auth | undefined>(undefined);
private static authGetter$ = AuthService.lastAuth$.pipe(
filter(auth => {
if (isNotUndefined(auth) && auth.expiry > new Date()) {
return true ; // identical resulting …
Run Code Online (Sandbox Code Playgroud) 我试图将以制表符分隔的文本文件中存储的公式加载到工作表中的某个范围。我已经使用该函数Split(Expression As String, Delimiter)
正确地将每一行依次加载到一维数组中,但是遇到了有关返回的数组类型的问题。
Split函数仅返回字符串类型的数组,而我需要一个变量类型数组来将范围设置为。这是因为使用字符串类型数组设置单元格的公式会导致将单元格值设置为原始文本,即使字符串以等号开头。
'Example code to demonstrate the problem:
Sub Tester()
Dim StringArr(1 To 3) As String
StringArr(1) = "= 1"
StringArr(2) = "= 2"
StringArr(3) = "= 3"
Range("Sheet1!$A$1:$C$1").Formula = StringArr
'Cells display raw string until edited manually
Dim VariantArr(1 To 3) As Variant
VariantArr(1) = "= 1"
VariantArr(2) = "= 2"
VariantArr(3) = "= 3"
Range("Sheet1!$A$2:$C$2").Formula = VariantArr
'Cells display formula result correctly
End Sub
Run Code Online (Sandbox Code Playgroud)
结果输出:
我想知道是否有一种方法可以将Split
函数返回的数组转换为变量类型数组,最好没有循环。我知道我可以在一个循环中分别设置每个单元格公式,但是我试图使其尽可能高效和整洁。
我正在使用Microsoft Excel for Mac 2011 …
标题几乎说明了一切。我有这个代码:
type testNoArgsF = () => number;
type testArgsF = (arg1: boolean, arg2: string) => number;
type unknownArgsF = (...args: unknown[]) => number;
type anyArgsF = (...args: any[]) => number;
type testII = testArgsF extends anyArgsF ? true : false; // true
type testIII = Parameters<testArgsF> extends Parameters<unknownArgsF>
? true
: false; // true
// unexpected:
type testIV = testArgsF extends unknownArgsF ? true : false; // false <- why?
// even though:
type testV = testNoArgsF extends unknownArgsF ? …
Run Code Online (Sandbox Code Playgroud) typescript ×2
arguments ×1
arrays ×1
cells ×1
excel-vba ×1
extends ×1
observable ×1
string ×1
typechecking ×1
typeguards ×1
types ×1
variant ×1