考虑这个例子:
function foo<T>(t: T): boolean {
  return t === 1;
}
Run Code Online (Sandbox Code Playgroud)
我明白了This condition will always return 'false' since the types 'T' and 'number' have no overlap.。
如果我将其更改为:
function foo<T extends any>(t: T): boolean {
  return t === 1;
}
Run Code Online (Sandbox Code Playgroud)
我认为T可能是any默认的,那我为什么需要extends any?没有extends any,TS 期望T是什么类型?
编辑,实际功能:
function cleanObj<T>(
  obj: ObjectOf<T>,
): ObjectOf<T> {
  const newObj = {};
  for (const k of Object.keys(obj)) {
    if (obj[k] !== null && typeof obj[k] …Run Code Online (Sandbox Code Playgroud) 我的配置是:
last 10 versions, not < 0.1%, not dead, not ie > 0, not op_mini all, not edge < 70, not samsung < 9
Run Code Online (Sandbox Code Playgroud)
我尝试删除not dead
last 10 versions, not < 0.1%, not ie > 0, not op_mini all, not edge < 70, not samsung < 9
Run Code Online (Sandbox Code Playgroud)
两者运行npx browserslist产生完全相同的结果:
and_chr 87
and_ff 83
and_qq 10.4
and_uc 12.12
chrome 87
chrome 86
chrome 85
chrome 84
chrome 83
chrome 81
chrome 80
chrome 79
chrome 77
edge 87 …Run Code Online (Sandbox Code Playgroud) 我有一个除了 之外没有其他道具的组件children,即:
function Foo({ children }: React.PropsWithChildren<>) {}
Run Code Online (Sandbox Code Playgroud)
React.PropsWithChildren需要论证。如果我这样做React.PropsWithChildren<{}>,Eslint 会生成Don't use `{}` as a type. `{}` actually means "any non-nullish value"..
我将它用于空对象类型:
type EmptyObj = { [k: string]: never };
Run Code Online (Sandbox Code Playgroud)
但是,React.PropsWithChildren<EmptyObj>导致:
Type '{ children: Element; }' is not assignable to type 'EmptyObj'.
  Property 'children' is incompatible with index signature.
    Type 'Element' is not assignable to type 'never'.
Run Code Online (Sandbox Code Playgroud)
我应该传递给React.PropsWithChildren什么?
编辑:
我知道我可以这样做:
function Foo({ children }: { children?: React.ReactNode }) {} …Run Code Online (Sandbox Code Playgroud) 我Object.entries像这样覆盖:
interface ObjectConstructor {
  entries<T extends Record<string, any>>(o: T): {
      [K in keyof T]: [K, T[K]];
  }[keyof T][];
}
Run Code Online (Sandbox Code Playgroud)
它通常工作正常。但是,在某些情况下,我得到:
Type '["alignContent", AlignContent | undefined] | ["alignItems", AlignItems | undefined] | ["alignSelf", AlignSelf | undefined] | ... 781 more ... | undefined' must have a '[Symbol.iterator]()' method that returns an iterator.
Run Code Online (Sandbox Code Playgroud)
联合中的最后一个类型是undefined,使我无法像这样解构:
const style: Partial<React.CSSProperties> = {
  height: '123px',
};
for (const [k, v] of Object.entries(style)) {
  ...
}
Run Code Online (Sandbox Code Playgroud)
根据类型定义,它永远不应该 return undefined。为什么它回来了undefined?我想省略undefined …
我有一个这样的类型Object.entries:
type _ObjectEntries<T extends Record<string, any>> = {
  [K in keyof T]: [K, T[K]];
}[keyof T][];
function objectEntries<T extends Record<string, any>>(
  obj: T,
): _ObjectEntries<{
  [K in keyof T]-?: T[K]
}> {
  return Object.entries(obj);
}
Run Code Online (Sandbox Code Playgroud)
当我知道物体的确切形状时,这非常有用,例如:
const obj1: { a: number, b?: number } = { a: 1, b: 2 };
const entry1 = objectEntries(obj1)[0]; // ["a", number] | ["b", number]
Run Code Online (Sandbox Code Playgroud)
尽管b是可选的,但这仍然有效。
但是,如果我不知道对象的确切形状(即它具有任意键),这会增加undefined该值:
const obj2: Partial<Record<string, number>> = { a: 1, b: 2 }; …Run Code Online (Sandbox Code Playgroud) 我有一个返回对象属性是否为 的函数undefined。我需要这个功能,而不是仅仅做,obj[key] === undefined因为否则有时我会得到Property 'foo' does not exist on type 'Bar'.. 当属性键是文字时,编写类型很简单。IE:
function hasDefinedProp<
  Obj extends Partial<Record<string, any>>,
  Prop extends string,
>(
  obj: Obj,
  prop: Prop,
): obj is Obj & Record<Prop, Prop extends keyof Obj ? Exclude<Obj[Prop], undefined> : unknown> {
  return obj[prop] !== undefined;
}
const obj: Partial<Record<string, number>> = {};
if (hasDefinedProp(obj, 'foo')) {
    obj.foo + 1; // No error.
    obj.bar + 1; // "Object is possibly 'undefined'."
}
Run Code Online (Sandbox Code Playgroud)
但是,当键的类型是宽类型时,这不起作用,即:
const …Run Code Online (Sandbox Code Playgroud) 考虑这些情况:
const obj = {};
obj ?? {};
obj?.prop;
Run Code Online (Sandbox Code Playgroud)
无效合并和可选链接都是不必要的,是否有 Eslint 规则来捕获这些?
我最近遇到了一个错误,我用的newVal === val ?? defaultVal不是newVal === (val ?? defaultVal). 这评估为boolean ?? defaultVal. 我认为任何其他 Eslint 规则都无法捕获此问题,因此类似no-unnecessary-condition之类的规则可能是我将来防止这种情况发生的最佳选择。
我有类似的东西:
$('.test').hover(
 function(){
  $(this).animate(...);
 }, function(){
  $(this).animate(...);
 }
);
Run Code Online (Sandbox Code Playgroud)
但是,如果用户的鼠标在动画结束前离开,则动画将继续.如果我快速重复地快速悬停和取消悬停元素,则在鼠标离开元素后动画会重复几次.鼠标离开元素后,如何让动画停止?
例如,如果我有example.com/experiments.html且用户输入example.com/experiment.html(否"s"),是否可以使用.htaccess将用户重定向到example.com/experiments.html ?
编辑:"experiments.html"只是一个例子,我有很多页面,用户可能输入错误.有通用的解决方案吗?
当我从数据库中获取数据时,即使它具有数字值,结果也是一个字符串.这就是我的意思:
// An integer
$int=10;
if(is_int($int)) // Returns true
...
// A string
$int='10';
if(is_int($int)) // Returns false
...
Run Code Online (Sandbox Code Playgroud)
我希望这两个都返回真实.
javascript ×8
typescript ×6
.htaccess ×1
babeljs ×1
eslint ×1
integer ×1
jquery ×1
mod-rewrite ×1
php ×1
reactjs ×1
webpack ×1