Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"resolutions": {
"jest": "^29.5.0",
"typescript": "^5.6.2",
"typescript": "^5.9.2",
"recast": "^0.23.1"
},
"repository": {
Expand Down Expand Up @@ -68,7 +68,7 @@
"tape": "^5.0.1",
"ts-jest": "^29.0.5",
"tsdx": "^0.14.1",
"typescript": "^5.6.2"
"typescript": "^5.9.2"
},
"husky": {
"hooks": {
Expand Down
27 changes: 27 additions & 0 deletions packages/mobx-react-lite/__tests__/observer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,33 @@ test("useImperativeHandle and forwardRef should work with useObserver", () => {
expect(consoleWarnMock).toMatchSnapshot()
})

test("observer should work with custom HOC and infer correct type", () => {
const validateChildrenWrapper = <T extends {}>(component: React.FC<T>): React.FC<T> => {
const wrapper: React.FC<T> = (...args) => {
const result = component(...args)
if (result && React.isValidElement(result) && result.type === "div") {
return result
}
throw new Error("Only <div> allowed as root element")
}
wrapper.displayName = component.displayName || component.name
return wrapper
}

interface IProps {
value: string
}

const TestComponent: React.FC<IProps> = observer(
validateChildrenWrapper(function TestComponent({ value }) {
return <div>{value}</div>
})
)

const rendered = render(<TestComponent value="1" />)
expect(rendered.container.querySelector("div")!.innerHTML).toBe("1")
})

it("should hoist known statics only", () => {
function isNumber() {
return null
Expand Down
11 changes: 5 additions & 6 deletions packages/mobx-react-lite/src/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,18 @@ export function observer<P extends object, TRef = {}>(
React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<TRef>>
>

export function observer<P extends object>(
baseComponent: React.FunctionComponent<P>,
options?: IObserverOptions
): React.FunctionComponent<P>

export function observer<P extends object, TRef = {}>(
baseComponent: React.ForwardRefExoticComponent<
React.PropsWithoutRef<P> & React.RefAttributes<TRef>
>
): React.MemoExoticComponent<
React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<TRef>>
>

export function observer<P extends object>(
baseComponent: React.FunctionComponent<P>,
options?: IObserverOptions
): React.FunctionComponent<P>

export function observer<
C extends React.FunctionComponent<any> | React.ForwardRefRenderFunction<any>,
Options extends IObserverOptions
Expand Down
38 changes: 38 additions & 0 deletions packages/mobx/__tests__/v5/base/typescript-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,44 @@ test("allow 'as const' for creating tuples for observable.map()", () => {
mobx.observable.map(newValues)
})

test("infers tuple type for observable.map() when using array.map", () => {
interface Usage {
daily: number
monthly: number
}
const data: Array<{ id: string; usage: Usage }> = [
{ id: "1", usage: { daily: 5, monthly: 100 } },
{ id: "2", usage: { daily: 10, monthly: 200 } },
{ id: "3", usage: { daily: 15, monthly: 300 } }
]

const map = mobx.observable.map(
data.map(app => [app.id, app.usage]),
{ name: "test" }
)

expect(map.get("2")).toEqual({ daily: 10, monthly: 200 })
})

test("observable.map() accepts an undefined value with or without options", () => {
const map = mobx.observable.map<string, number>(undefined, { name: "custom name" })
map.set("test", 1)
expect(map.get("test")).toBe(1)
expect(map.name_).toBe("custom name")

// without options
mobx.observable.map(undefined)
})

test("observable.map() accepts optional initial values", () => {
interface Usage {}

;(data?: [string, Usage][]) => observable.map(data, { name: "test" })
;(data?: readonly (readonly [string, Usage])[]) => observable.map(data, { name: "test" })
;(data?: Record<string, Usage>) => observable.map(data, { name: "test" })
;(data?: Map<string, Usage>) => observable.map(data, { name: "test" })
})

test("toJS bug #1413 (TS)", () => {
class X {
test = {
Expand Down
26 changes: 22 additions & 4 deletions packages/mobx/src/api/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
IEqualsComparer,
IObservableArray,
IObservableMapInitialValues,
IMapEntries,
IReadonlyMapEntries,
IKeyValueMap,
IObservableSetInitialValues,
IObservableValue,
ObservableMap,
Expand Down Expand Up @@ -151,6 +154,24 @@ export interface IObservableValueFactory {
<T>(value?: T, options?: CreateObservableOptions): IObservableValue<T | undefined>
}

export interface IObservableMapFactory {
<K = any, V = any>(): ObservableMap<K, V>
<K, V>(initialValues?: IMapEntries<K, V>, options?: CreateObservableOptions): ObservableMap<
K,
V
>
<K, V>(
initialValues?: IReadonlyMapEntries<K, V>,
options?: CreateObservableOptions
): ObservableMap<K, V>
<K, V>(initialValues?: IKeyValueMap<V>, options?: CreateObservableOptions): ObservableMap<K, V>
<K, V>(initialValues?: Map<K, V>, options?: CreateObservableOptions): ObservableMap<K, V>
<K = any, V = any>(initialValues: undefined, options?: CreateObservableOptions): ObservableMap<
K,
V
>
}

export interface IObservableFactory
extends Annotation,
PropertyDecorator,
Expand All @@ -172,10 +193,7 @@ export interface IObservableFactory
initialValues?: IObservableSetInitialValues<T>,
options?: CreateObservableOptions
) => ObservableSet<T>
map: <K = any, V = any>(
initialValues?: IObservableMapInitialValues<K, V>,
options?: CreateObservableOptions
) => ObservableMap<K, V>
map: IObservableMapFactory
object: <T = any>(
props: T,
decorators?: AnnotationsMap<T, never>,
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx/src/types/observablemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface IKeyValueMap<V = any> {
export type IMapEntry<K = any, V = any> = [K, V]
export type IReadonlyMapEntry<K = any, V = any> = readonly [K, V]
export type IMapEntries<K = any, V = any> = IMapEntry<K, V>[]
export type IReadonlyMapEntries<K = any, V = any> = IReadonlyMapEntry<K, V>[]
export type IReadonlyMapEntries<K = any, V = any> = readonly IReadonlyMapEntry<K, V>[]

export type IMapDidChange<K = any, V = any> = { observableKind: "map"; debugObjectName: string } & (
| {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13778,10 +13778,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==

typescript@^3.7.3, typescript@^5.6.2:
version "5.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.2.tgz#d1de67b6bef77c41823f822df8f0b3bcff60a5a0"
integrity sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==
typescript@^3.7.3, typescript@^5.9.2:
version "5.9.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.9.2.tgz#d93450cddec5154a2d5cabe3b8102b83316fb2a6"
integrity sha512-CWBzXQrc/qOkhidw1OzBTQuYRbfyxDXJMVJ1XNwUHGROVmuaeiEm3OslpZ1RV96d7SKKjZKrSJu3+t/xlw3R9A==

uglify-js@^3.1.4:
version "3.17.4"
Expand Down
Loading