AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-1068446

dwjohnston's questions

Martin Hope
dwjohnston
Asked: 2025-04-15 11:33:04 +0800 CST

Required<T> 不适用于辅助查找类型

  • 5

如果我有一些类似这样的代码:

type Bravo = {
  b?: string; 
}

type Charlie = {
  c:Bravo['b']
}

function acceptsCharlie(value: Charlie){
  //'value.c' is possibly 'undefined'.(18048)
  value.c.split('');
}

这是有道理的——该类型Charlie从继承了可选字符串Bravo。

我们可以通过使用Required实用程序类型来解决这个问题:

type Bravo = {
  b?: string; 
}

type Charlie = {
  //  👇
  c:Required<Bravo>['b']
}

function acceptsCharlie(value: Charlie){
  // no error here 
  value.c.split('');
}

简单的。

但是,如果我再添加一层键入查找:

type Alpha = {
  a?: string; 
}

type Bravo = {
  b: Alpha['a']; 
}

type Charlie = {
  c: Required<Bravo>['b']
}

function acceptsCharlie(value: Charlie){
  //'value.c' is possibly 'undefined'.(18048)
  value.c.split('');
}

这Required不再能解决我的问题。

怎么回事?

typescript
  • 2 个回答
  • 36 Views
Martin Hope
dwjohnston
Asked: 2025-03-24 15:04:11 +0800 CST

TypeScript 为何省略未使用的导入的规范参考

  • 4

如果你有如下代码:

//other.js|ts

console.log("module sideffect") 
export function foo() {

}


//main.js|ts 
import {foo} from "./other"; // foo is not used  

console.log("do something else"); 

那么 JavaScript 和 TypeScript 在这种情况下的行为会有所不同。

TypesScript 将省略 的导入"./other",并且console.log("module sideeffect")将不会触发。

而 JavaScript 将执行导入并且它将触发。

另一方面,如果我们进行如下导入:

import "./other"; 

模块的副作用将会激发。

这里有一个相关问题:

导入语句中的副作用

还有这里:

导入仅针对节点的 Typescript 模块以产生副作用

两者都在谈论这个问题,但不幸的是,两个答案现在都指向不再存在的 TypeScript 文档。

我正在寻找 TypeScript 为何会省略此内容的规范原因,以及这是否是可配置的行为。(可以想象这样一种情况:由于不再使用导入,代码停止工作)。

typescript
  • 1 个回答
  • 37 Views
Martin Hope
dwjohnston
Asked: 2025-02-07 08:00:07 +0800 CST

MDN 文档中指定的返回类型是否表示除非另有说明,否则值将始终存在?

  • 5

我正在寻找cookieStore.get函数的 MDN 文档。

CookieStore尚未被 TypeScript 类型化,所以我需要创建自己的定义。

我的问题是,在 MDN 文档中,MDN 表示函数的可选参数 - 但在返回类型上,我还没有看到返回类型的可选值。这是否意味着我可以假设cookieStore.get应该/将存在上列出的所有值?

cookiestore
  • 1 个回答
  • 24 Views
Martin Hope
dwjohnston
Asked: 2024-12-16 07:21:43 +0800 CST

为什么我不能通过 React 中存储的 ref 解析函数来解决承诺?

  • 4

我正在尝试创建一个 promisify 样式的钩子,想法是最终将它与 Redux Toolkit 一起使用,以允许使用新的 React 19use钩子。

以下是这个东西的工作原理:

function usePromise(input: {
    isLoading: true,
    data: null
} | {
    isLoading: false,
    data: string;
}) {


    // Store the resolve function in a ref 
    const resRef = useRef<(data: string) => void>(null);

    // Create a new promise 
    // Store the resolve function in the ref 
    const promiseRef = useRef(
        new Promise((res) => {
            resRef.current = res;

            //res("xxx") // 👈 this will resolve though
        })
    );

    // When input changes, if there is data, resolve the promise
    useEffect(() => {
        if (!input.isLoading) {
            resRef.current?.(input.data);
        }

    }, [input]);

    // Return the promise 
    return promiseRef.current;
}

用法如下:

export function MyComponent() {

    const [value, setValue] = useState<null | string>(null);

    const prom = usePromise(value ? {
        isLoading: false,
        data: value
    } : {
        isLoading: true,
        data: null
    });

    prom.then((v) => alert(v))

    return <div >

        <button onClick={() => setValue("123")}>Click me</button>
    </div>
}

在这里,我期望当我们点击按钮时,承诺会得到解决,我们会看到警报。然而,事实并非如此。

这里发生了什么事?

我在这里复制了此问题:https ://github.com/dwjohnston/react-promise-issue

reactjs
  • 1 个回答
  • 42 Views
Martin Hope
dwjohnston
Asked: 2024-10-03 10:14:24 +0800 CST

我可以让 TypeScript 类型保护针对函数重载的结果应用吗?

  • 5

我有一些如下所示的代码:

type UserTypes = "user" | "super-user" | "admin"  ;


function getUserType() : UserTypes {

}

function getAdminPrivs(userType: "user" | "super-user") : null  
function getAdminPrivs(userType: "admin") :  string 
// nb. we need to double declare the final line
// see: https://stackoverflow.com/questions/70146081/why-does-an-overloaded-function-declaration-sometimes-force-useless-type-narrowi
function getAdminPrivs(userType: UserTypes) : null | string
function getAdminPrivs(userType: UserTypes) : null | string {

  if(userType === "admin"){
    return "hello"
  }

  return null; 
}


// "user" | "super-user" | "admin"
const userType = getUserType();

// string| null  
const result = getAdminPrivs(userType);


if(userType === "user" || userType === "super-user"){

}
else {
  // string | null 
  // I really want this to be just string
  result
}

在这里,我希望 TypeScriptresult通过检查 的类型来缩小 的类型userType。这可能吗?

举个例子,如果我稍微变换一下代码:

const userType = getUserType();
if(userType === "user" || userType === "super-user"){
  // null
  const result = getAdminPrivs(userType);
}
else {
  //string 
  const result = getAdminPrivs(userType);
}

这在语义上是类似的,但是我们获得了类型缩小的优势。(我无法在实际用例中使用这种技术,因为它getAdminPrivs实际上是一个 React hook,并且不允许有条件地调用 React hook)

typescript
  • 1 个回答
  • 29 Views
Martin Hope
dwjohnston
Asked: 2024-06-25 09:18:24 +0800 CST

使用 TypeScript 中的转换映射重命名对象的属性?

  • 6

我正在尝试实现这样的目标:

const input = {a: 1, b: "hello"}; 
const result = renameProperties(input,{a: "alpha", b: "beta"}); 
result.alpha // number
result.beta // string
result.foo //Property 'foo' does not exist on type... 

所以我的转换函数如下所示:


export function renameProperties<
  TInput extends Record<string, unknown>,
  TConversionMap extends Record<keyof TInput, string>,
  TOutput extends // heres where I get stuck 
>(input: TInput, conversionMap: TConversionMap): TOutput {

}

作为第一步我已经得到:

TOutput extends {[K in TConversionMap[keyof TInput]] : string}

这不完全符合我的要求——我希望结果只包含转换后的键。

result.alpha // string
result.beta // string

result.foo //string  - but really was hoping to see an error here 

第二部分将涉及保留输入的价值,我正在考虑类似的事情:

TOutput extends {[K in TConversionMap[keyof TInput]] : TInput[The Input key, if we can retain a reference to it]}

我该怎么做?这可能吗?

typescript
  • 1 个回答
  • 15 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve